c# - Reflection and dynamic in-memory assemblies -
Tell us that I dynamically create a type using CSharpCodeProvider and does not choose to continue the results I am Generated assembly is present in memory only, we say that I create two types of two separate in-memory assemblies:
Assembly 1:
Public category DynamicTypeA {}
Assembly 2:
Public category DynamicTypeB {Public DynamicTypeA MyProperty {get; Set; }}
As you can see that the second type is the first type of property. cold. Now I want to find the dynamic type-beb using reflection:
foreach (Typing (DynamicTypeB) in PropertyInfo PI. GETProperties ()) {Console.WriteLine (pi.PropertyType.Name ); }
It is found that PropertyInfo.PropertyType fails when the conference is not located on the disc !!! This is true for building member Infos and all other types of inquiries.
As we all know, the NET API is using type input on the backend and when it has been checked, then it fails to stay inside of any type. -Momiri Assembly for example expression. The member takes Infos as the first parameter and is using it to verify that the type of expression that is provided in the second parameter matches the member's type. When this type occurs in the memory assembly expression. The stop fails.
Can anyone think about the solution?
Creating and dynamically writing types creates pollutants and it is bad, yet these types of returns are not.
Thank you
It turns out that PropertyInfo.PropertyType fails when conference is not located on disk
Are you sure? Take a look:
Fixed zero main (string [] args) {string code = @ "namespace foo {public class DynamicTypeA {} public class DynamicTypeB {public DynamicTypeA MyProperty {get; set;} }} "; CSharpCodeProvider CSP = New CSharpCodeProvider (); Compiler parameter P = new compiler parameter (); P.GenerateInMemory = True; Var result = csp.CompileAssemblyFromSource (P, code); Forward (Type Type Result. Compact Assets GateType ()) {console.lightline (type name); Foreground (PropertyInfoPI TypeGetProperties ()) {Console.WriteLine ("\ t {0}", pi.PropertyType.Name); }} Console. Readline (); }
This uses your snippet and works like a magic
Moving the loop inside the dynamic code does not change a lot, it does not change Still works:
usingstring code = @ "system; using namespace namespoo {public category DynamicTypeA} public class DynamicTypeB {public DynamicTypeA MyProperty {get; set; }} Public class DynamicTypeC {public void Foo () {foreach (GGetProperties (typing (DynamicTypeB)) {Console.WriteLine (Pi.PropertyType.Name);}}}} "; CSharpCodeProvider CSP = New CSharpCodeProvider (); Compiler parameter P = new compiler parameter (); P.GenerateInMemory = True; Var result = csp.CompileAssemblyFromSource (P, code); Var c = results.CompiledAssembly.CreateInstance ("foo.DynamicTypeC"); Var type C = c.GetType (); TypeC.InvokeMember ("Foo", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, c, null);
If you have problems here for some reasons, then you are definitely doing some more complex.
Comments
Post a Comment