Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,128 | Comments: 45,548

filter by tags archive

The difference between meta programming and IL weaving

time to read 1 min | 191 words

Ivan also posted a response to Jeremy's C# vNext features, and he said something that caught my eye:

5. Metaprogramming => no objection here although you can do it now with reflection emit

No, there is a big difference between reflection emit and byte code weaving. Reflection emit is done after compilation is completed, meta programming occurs during compilation. This matters because it means that your code cannot refer to the results of the change being made.

A case in point, I am using IL Weaving to add the Dispose method to a class, but because I am doing it after compilation is completed, I cannot call the Dispose() method on the class, or put it in a using statement, in the same assembly, I would get a compiler error, because the method (and the interface) are not there yet.

Using meta programming, the compiler will know that those now exist, and it can not start using it, because it happened during the compilation process.

The implication of that are pretty significant, if you are talking about what you can and can't do in terms of enriching the language.


Scott Bellware

I know this is probably such an obvious comment to make that it might just be completely lame, but...

All these rules are different in a dynamic language like IronRuby, where ensuring the presence of invocation sites is done at run time... or where you might be able to add the implementation of IDisposable at runtime by calling Dispose().

I've heard you say that all you need is an IDynamicObject interface and support for it in the runtime to achieve what Ruby can achieve.

Not knowing the depths of the details of what you have in mind, would something like IDynamicObject.MethodMissing() give you what you need? Does it mean that the compiler would switch to a duck typing mode when it encounters a class that implements IDynamicObject?

Ayende Rahien

IDynamicObject.MethodMissing would go a long way, but it solves only a part of the problem, actually.

Adding members, interfaces, etc, to the code is just as important.

That is not something that you can do with a method missing.


And what about post-compilation weaving such as done by PostSharp? Granted, still not the first-class support achievable with meta-programming, but goes a long way to improve upon runtime weaving.

Scott Bellware

That is not something that you can do with a method missing

If a member can be added to an object at runtime, it could be added from MethodMissing.

It would seem like C# would need duck typing regardless of where new members are added from at runtime.

Is duck typing out of bounds for what you envision for a meta programming solution?

Ayende Rahien


No, post compile is not sufficient, that is basically IL Weaving, and it is not working, because you have no way to refer to the results of this method.

Ayende Rahien


Given meta programming, I can add duck typing, the other way around is where I get into trouble.

Comment preview

Comments have been closed on this topic.


  1. The worker pattern - 3 days from now

There are posts all the way to May 30, 2016


  1. The design of RavenDB 4.0 (14):
    26 May 2016 - The client side
  2. RavenDB 3.5 whirl wind tour (14):
    25 May 2016 - Got anything to declare, ya smuggler?
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series



Main feed Feed Stats
Comments feed   Comments Feed Stats