简体   繁体   中英

Type.GetType() returns null

These all return null, I'd expect the first one to work:

 Type.GetType("Namespace.To.My.Type, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
 Type.GetType("Namespace.To.My.Type, MyAssembly")
 Type.GetType("Namespace.To.My.Type")

So, in debugging this, I tried this:

var aqn = AppDomain.CurrentDomain.GetAssemblies()[92].GetType("Namespace.To.My.Type").AssemblyQualifiedName; // Namespace.To.My.Type, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Type.GetType(aqn)

(where 92 is MyAssembly )

which also returns null.

What going on? Is my assembly only half-loaded or something? Surely not, because I can do this:

AppDomain.CurrentDomain.GetAssemblies()[92].GetTypes()[161]

and find Namespace.To.My.Type , and also

AppDomain.CurrentDomain.GetAssemblies()[92].GetType("Namespace.To.My.Type")

Updates

  • Worth noting that MyAssembly has been loaded via Assembly.Load(path) immediately before the above code
  • I'm in a console app
  • the AQN of my type is shown above in a code comment, scroll right
  • If i make GetType throw, it throws: "Could not load file or assembly 'MyAssembly' or one of its dependencies. The system cannot find the file specified."

Fuslogvw output:

FusionLog: "=== Pre-bind state information ===\\r\\nLOG: DisplayName = Namespace.To.My.Type, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\\n (Fully-specified)\\r\\nLOG: Appbase = file:///MyConsoleAppPath/bin/Debug/\\r\\nLOG: Initial PrivatePath = NULL\\r\\nCalling assembly : MyConsoleApp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.\\r\\n===\\r\\nLOG: This bind starts in default load context.\\r\\nLOG: Using application configuration file: D:\\MyConsoleAppPath\\bin\\Debug\\MyConsoleApp.exe.Config\\r\\nLOG: Using host configuration file: \\r\\nLOG: Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\config\\machine.config.\\r\\nLOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).\\r\\n

This is the answer: Is it possible to use Type.GetType with a dynamically loaded assembly?

its because I'd loaded the assembly by Assembly.Load - although I still dont understand why this causes the observed problem

I already had this problem. The framework of the assembly and that of the project did not correspond, it is perhaps your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM