about Reflection.Emit

Hi,

I want to insert new classes and attributes into an existing assembly. Reflection.Emit can be used to modify assemblies dynamically isn't. So can I use it to achieve my purpose

Thank you.


Answer this question

about Reflection.Emit

  • Ewan

    Yes and no. You can dynamically create new types and load them into the current appdomain. You can't change an existing assembly. In order to change the assembly you'd have to recompile the whole thing. Theoretically you could use reflection to get all the types in the assembly and then add new types and recompile but any assembly-level attributes (like signed keys and resources) would be lost.

    I don't see any benefit in attempting to modify an existing assembly. If you want to do interception then I'm afraid you won't be able to accomplish it easily in .NET due to the security constraints .NET enforces.

    Also as of v2.0 you can create new types and load them efficiently using the lightweight code generator but again it'll simply be a new type in the appdomain and not have any impact on existing assemblies.

    Michael Taylor - 1/30/06


  • Emil Helldin


    Hi,
    Thanks for the reply. Reflection.Emit can be used to in-memory assemblies but cannot be used to change existing assemblies. Unmanaged Metadata API can be used to only change metadata & not the whole assembly isn't I need to insert new classes,attributes into an existing assembly.

    There are a certain APIs available like Cecil, RAIL, PEAPI etc but Iam currently checking which one is the best one for my purpose. Any suggestions are welcome.

    Thank you.



  • about Reflection.Emit