views:

59

answers:

3

I realize this is a very strange question. Let me just say that I have my reasons. (I tend to write very long, wordy questions; I want to keep this one short.)

If I have some type T, can I use reflection to define a new type, let's call it T2, that will basically be identical to T? What I'm looking for is essentially a dynamic way to do the equivalent of what would result if I wrote the exact same code (except for class name) in two separate source code files and compiled both.

+2  A: 

I don't think there's a magic bullet method that will do your work for you, but you can do it with Reflection Emit.

Here's a nice tutorial on parts of the subject. Start there then move up to some of the sister articles.

Randolpho
That is 50% of the problem. But how does he get the code *out* of the target type?
Kirk Woll
@Kirk Woll: are you sure that's what he wants? My read of the question is that he wants to dynamically generate and use the type, not create code.
Randolpho
@Randolpho, my take is that the OP wants to take a System.Type and create a new System.Type that is a mirror of the original. Since no source code would be involved in this process, I'm curious where he would get the IL to emit?
Kirk Woll
@Randolpho, @Kirk: You guys are both right. I think Kirk's point is that your answer doesn't address the issue of extracting the more detailed type information (e.g., the IL for the constructors, methods, etc.) from the source type to emit to the target type. However, this is an excellent starting point and has proven helpful already.
Dan Tao
Just had one of those Ahah! moments. I thought @Kirk Woll was trying to generate C# code from the created type... he meant getting the execution out of the original method. Duh!
Randolpho
@Dan Tao: the answer to that is... wrap it and call the original. The original type isn't exactly going away.
Randolpho
+1  A: 

Randolpho's links should point you in the right direction. As for emitting identical functionality, you can retrieve the IL from methods by calling X.GetMethodBody().GetILAsByteArray() where X is a given instance of MethodInfo. That would still need to be translated and re-emitted in your new method, though, because as far as I know there isn't a direct way to write an IL stream of byte[] to a particular ILGenerator as IL.

Haibo Luo had a blog post about this from 2005 that may point you in the right direction for translation (I haven't tried it out so I can't verify that it works).

Chris Hannon
A: 

You can copy structure of type relying on System.Reflection.Emit types + traverse and clone method bodies with tools like MethodBodyReader or CCI or Cecil. But what if method in source type was using some internal field or calling internal method, or maybe using property of outer class (in case of inner class)... As as result you will get invalid code. To correct it, your newly created type should be placed right near the original type. Luckily Cecil and CCI can do this trick.

desco