all groups > dotnet clr > september 2005 >
You're in the

dotnet clr

group:

Using TypeBuilder and System.Reflection.Emit to generate an assembly



Using TypeBuilder and System.Reflection.Emit to generate an assembly Dominic Souvoigne
9/10/2005 3:17:36 PM
dotnet clr: Hi,



I have a compiled assembly: source.dll.



I would like to add a single method



public static string version(){

return "1.0.1.1";

}



to all the types in the assembly, maintaining type names, so the client code
wouldn't have to change.





I can traverse all the types in the assembly using:



Assembly a = Assembly.LoadFrom("source.dll");

Type[] mytypes = a.GetTypes();





Now I have info on all the types in the assembly.



And I can create my new type using



myAssemblyBuilder = myCurrentDomain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.Save);

ModuleBuilder mBuilder =
myAssemblyBuilder.DefineDynamicModule("SourceModule","source.dll");

TypeBuilder builder = mBuilder.DefineType



But, I am not able to find the way to use modify source types.



I could inherit from source types, since DefineType method accepts BaseType
parameter but this does not resolve my problem, since I would like to
REPLACE original source.dll:



myAssemblyBuilder.Save("source.dll");



Help, please?



Thank you,



Dominic


Re: Using TypeBuilder and System.Reflection.Emit to generate an assembly Nick Hertl
9/12/2005 11:17:54 AM
For most tasks, there are multiple ways to complete it; often some of
those ways are harder than others. I believe that you have chosen a
difficult way to solve this problem.

It sounds like you have some source that you used to create this code.
If that is true, than the clear choice is to write a text parsing
script to just add the appropriate method in the source and then
recompile.

If you truly just have the assembly and you want to add a method to
each type in that assembly, the job is very difficult to do correctly,
and here is why.

System.Type can be retrieved easily from an assembly, but not modified.
If you want to make a new Type, you need to start with a TypeBuilder,
emit all the stuff you need, and then call TypeBuilder.CreateType on
that TypeBuilder to get your new Type. I have seen some code to do
this, but it's quite complicated because you basically have to write
code to fully compile the new assembly including all the existing
Fields, Methods, Attributes, and probably more. If you want debugging
to still be possible with your new assembly, it will be even more
complicated because you will need to re-emit all the pdb information
too using the APIs in ISymWrapper.dll which are also a pain to use.

Alternatively, you may consider dealing with your versioning issues in
some other way. Perhaps you could use the AssemblyVersionAttribute or
the AssemblyFileVersionAttribute to set and check which version of the
assembly you are using.

Also, although I haven't used it, maybe this tool could be of use to
you: http://research.microsoft.com/~mbarnett/ilmerge.aspx
AddThis Social Bookmark Button