"System.IO.FileNotFoundException:" Could not load file or assembly ' ClassLibrary2, Culture=neutral, publickeytoken= Null '. The system cannot find the file specified. ""
In. NET program development we will often use reflection, and recently began to touch slowly. Netcore. The syntax for Core reflection is the same, so it's fine to write it down, but as soon as you run it, it pops up the beginning of the exception.
The project structure is as follows
ClassLibrary3 is an interface
namespace classlibrary3{ publicinterface IClass1 { void Print (); }}
ClassLibrary2 is the realization of ClassLibrary3
namespace classlibrary2{ publicclass class1:iclass1 { public void print () { Console.WriteLine ("print ... .. " ); } }}
The main program gets to the type and creates the object through reflection
classProgram {Static voidMain (string[] args) {Console.WriteLine ("Hello world!"); Assembly Assembly= Assembly.Load (NewAssemblyName ("ClassLibrary2")); Type type= assembly. GetType ("Classlibrary2.class1"); varInstance =(ICLASS1) activator.createinstance (type); Instance. Print (); Console.ReadLine (); } }
This code is not a problem here, but when executed, it throws the opening exception. After careful examination, the DLL acknowledgement generated by ClassLibrary2 has been copied to the main program directory.
The first contact with the core is not familiar with the system, finally after a multi-party effort, found under the main program project has a Deps.json file
The contents of the file are like this (there is no ClassLibrary2 because we have not referenced ClassLibrary2 in the project)
And then think of the exception hint can not find the path, then we will be configured here soon?
Then change to the following:
At this time we run the program:
Perfect!
. Netcore Reflection: Could not load file or assembly system cannot find the specified file