c# - Should Type.GetType(string) be aware of dynamically generated types? -


i have app creates code using codedom compiler. can see generated assembly in memory. when call type.gettype(typename), returns null. find little bit confusing.

what doing wrong?

static void main(string[] args) {     // fyi: code dummy class 1 instance method.     string code = system.io.file.readalltext("codetocompile.cs.txt");      string errors = null;     assembly asm = dynamiccompiler.compile(code, generateinmemory: true, generatedebuginfo: false, message: ref errors);      // type generated assembly. know there one.     type oneandonlytypeinassembly = asm.gettypes().first();      string typename = oneandonlytypeinassembly.assemblyqualifiedname;      // tell type system return instance of type based on qualified name.     // i'd expect work, since assembly loaded memory.     type sametype = type.gettype(typename);      if (sametype != null)     {         console.writeline("type found , equal={0}", oneandonlytypeinassembly.equals(sametype));     }     else     {         console.writeline("type not found");     } } 

please see remarks section in msdn. want not supported:

gettype works on assemblies loaded disk. if call gettype type defined in dynamic assembly defined using system.reflection.emit services, might inconsistent behavior. behavior depends on whether dynamic assembly persistent, is, created using runandsave or save access modes of system.reflection.emit.assemblybuilderaccess enumeration. if dynamic assembly persistent , has been written disk before gettype called, loader finds saved assembly on disk, loads assembly, , retrieves type assembly. if assembly has not been saved disk when gettype called, method returns null. gettype not understand transient dynamic assemblies; therefore, calling gettype retrieve type in transient dynamic assembly returns null.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -