问题
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