all groups > dotnet clr > june 2004 >
You're in the

dotnet clr

group:

How do I re-write an assembly's metadata?


How do I re-write an assembly's metadata? Ricardo
6/18/2004 10:12:01 AM
dotnet clr: I need to open an assembly, change some custom attribute's values and then re-write the assembly without re-compiling it.

I tried using Reflection.Emit, but I couldn't cast (System.Reflection.)Assembly to (System.Reflection.Emit.)AssemblyBuilder, which is the class where custom attributes are writeable (in System.Reflection.Assembly they're just readable) and has a "save" method. What's the difference between a dynamic assembly and a common one?? I guess a dynamic assembly is one changed using Reflection.Emit, not persisted, and loaded using CreateInstance

A friend recommended me to use CodeDom, but that's not what I need, I don't want to depend on the source code and re-compile the assembly, what I want is to change the assembly with no re-compiling.

I also read some info about this IMetaDataImport.IMetaDataAssemblyImport interface which is exposed via IMetaDataDispenser interface, and provides a way to change assembly's metadata and persist this changes, but I do not have enough documentation. I would appreciate some help, source code samples, whatever...

Thanks a lot!

RE: How do I re-write an assembly's metadata? Shri Borde [MS]
6/19/2004 12:10:48 AM
Something that will work for simple cases is to round-trip through ildasm
and ilasm. Use ildasm, manually change the IL to do what you want,
including adding a CustomAttribute, and then recompile with ilasm.

Shri Borde [MSFT]

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
[quoted text, click to view]
(System.Reflection.)Assembly to (System.Reflection.Emit.)AssemblyBuilder,
which is the class where custom attributes are writeable (in
System.Reflection.Assembly they're just readable) and has a "save" method.
What's the difference between a dynamic assembly and a common one?? I guess
a dynamic assembly is one changed using Reflection.Emit, not persisted, and
loaded using CreateInstance
[quoted text, click to view]
don't want to depend on the source code and re-compile the assembly, what I
want is to change the assembly with no re-compiling.
[quoted text, click to view]
interface which is exposed via IMetaDataDispenser interface, and provides a
way to change assembly's metadata and persist this changes, but I do not
have enough documentation. I would appreciate some help, source code
samples, whatever...
[quoted text, click to view]
Re: How do I re-write an assembly's metadata? Fabian Schmied
6/19/2004 9:58:35 AM
On Fri, 18 Jun 2004 10:12:01 -0700, Ricardo
[quoted text, click to view]

Yes, this is not possible.

[quoted text, click to view]

A dynamic assembly has been created using Reflection.Emit. A non-dynamic
assembly cannot be converted to a dynamic assembly (unless you manually
copy each module, type, and member).

[quoted text, click to view]

IMetaDataImport, -Emit, -AssemblyImport, -AssemblyEmit, and -DispenserEx
are documented in the .NET SDK (Tool Developers Guide\Docs). I tried to
use the IMetaDataDispenserEx interface for loading modules some time ago,
that works fine. You can use IMetaDataEmit to add or change some custom
attributes, no problem (removing them would be more difficult). Then, you
can use IMetaDataEmit to save the modules to files. However this will only
save the metadata, not the PE file layout, managed code, and unmanaged
code, so it's probably not what you want. Unless you do the rest yourself,
that is.

Fabian

--
AddThis Social Bookmark Button