[Mono-devel-list] Reflection[.Emit] / Copy an Assembly

Adam Chester a.chester at sph.uq.edu.au
Mon Apr 28 14:26:14 EDT 2003


Dear Mono people,

For now, I'm attempting this on the MS runtime, sooner or later I need
to make it work on Mono.

Background:
I need to create a copy of a dynamic assembly (one created in memory),
however the copy must be modifiable first. That is, I need to add
properties and members and attributes to any System.Type in the newly
created copy of the original assembly. Then using Reflection.Emit (or
another API such as CodeDom+Compile) to create the new improved
assembly, which does not need to be saved to disk (but could be if
necessary).

Problem:
So, after various failed attempts at doing this, the only small problem
left now is that I need a way of copying the dynamic assembly's
metadata, in particular I am concerned about Custom Attributes. I would
need to get the attribute instantiation information, rather than the
more easily obtainable (from Reflection) instance of the custom
attribute.

In reflection, myType.GetCustomAttributes() returns an object array of
instances of the attributes, however this is not what I want. I need to
get the information necessary to to create (for example) a
System.Reflection.Emit.CustomAttributeBuilder, which generally means
I'll need to know which constructor was called on the attribute, and the
parameter values that were given to it.

You will see an example of the format in an MS-Generated assembly for a
custom attribute:

.custom instance void
[System.Xml]System.Xml.Serialization.XmlTypeAttribute::.ctor() = ( 01 00
02 00 54 0E 09 4E 61 6D 65 73 70 61 63 65   // ....T..Namespace ...

There is a constructor signature there, and some binary formatted data
which provides parameter values (presumably) to the constructor, so the
runtime can create instances of the attributes.

I need to take this custom attribute information out of a generated
dynamic assembly using reflection or some other mechanism, and then put
the same information into a
System.Reflection.Emit.CustomAttributeBuilder instance. There seems to
be allowance for doing this (Eg.
Module.SetCustomAttribute(System.Reflection.ConstructorInfo, byte[])),
but I don't quite understand the binary (byte[]) format necessary.

Thank you very much in advance for your help,

- Adam Chester


-----
Adam Chester (a.chester at sph.uq.edu.au)
Computer Programmer / Database Developer
HISDU : http://www.sph.uq.edu.au/hisdu/
School of Population Health (http://www.sph.uq.edu.au)
University Of Queensland (http://www.uq.edu.au)

Phone: +61 7 33655530
Mobile: +61 411 752520




More information about the Mono-devel-list mailing list