C# Reflection: Emitting classes to existing assemblies

偶尔善良 提交于 2019-12-22 09:53:55

问题


Is it possible to use Reflection.Emit to create types in an existing assembly, or do you need to define a new dynamic assembly to be able to contain dynamic types?

Basically, I intend to read in one XML definition file which defines a class that is then instantiated multiple times and populated with the data from several other XML files. Rinse and repeat over several folders (each with a different definition file).

I also intend to use this data to dynamically build the interface to my app as well as define how the data is formatted when re-saving the data to the XML files.


回答1:


When using Reflection.Emit, you must always create a new assembly.

However, one potential option is to take your existing assembly, and define the contracts as interfaces. You can create the new assembly at runtime with Reflection.Emit, and have it define types that implement your interfaces. Your assembly can use a factory pattern to instantiate the property, dynamically generated type, and return an implementation of your (known at compile time) interface.




回答2:


Using Reflection.Emit, you define a new assembly.



来源:https://stackoverflow.com/questions/2114501/c-sharp-reflection-emitting-classes-to-existing-assemblies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!