One, what is reflection?
1,classes in the System.Reflection namespace and System.Type enable you to get information about the loaded assembly and the types it defines, such as classes, interfaces, and value types. You can use reflection to create, invoke, and access type instances at run time.
2,system. Type class is central to reflection. " The >system.type class plays a central role in reflection. When the reflection request loads the type, the common language runtime creates a type for it. You can use the methods, fields, properties, and nested classes of the Type object to find all the information about that type.
system. Type class is central to reflection. " >3, dynamically creating instances of types, anchoring types to existing objects, or getting types from existing objects (dynamically getting assemblies)
system. Type class is central to reflection. " >4, an application needs to load a specific type from a particular assembly at run time, so that reflection can be used to implement a task;
Second, the design of simple factory
References between the various layers of the project:
Fanshelk Reference Ifanshelk
DATABLL references Fanshelk,ifanshelk and Datademo
Datademo Reference Ifanshelk
Mvctest Reference DATABLL
Three, the code is as follows
Fanshelk Code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingIfanshelk;namespacefanshelk{ Public classClass1:iclass1 { Public voidAA () {Console.WriteLine ("AA"); } }}
Ifanshelk's Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ifanshelk{ publicinterface IClass1 { void AA (); }}
Datademo Code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingIfanshelk;usingSystem.Reflection;namespacedatademo{ Public Sealed classDataAccess {Private Static ReadOnly stringAssemblyPath ="Fanshelk"; Publicdataaccess () {}#regionCreateObject//do not use cache Private Static ObjectCreateobjectnocache (stringAssemblyPath,stringclassnamespace) { Try { //use Assembly to define and load an assembly, load the modules listed in the assembly manifest, and locate a type in this assembly and create an instance of it. ObjectObjType =Assembly.Load (assemblypath). CreateInstance (Classnamespace); returnObjType; } Catch(Exception e) {returnE.message; } } #endregion /// <summary> ///creates a data-tier interface. /// </summary> Public StaticIClass1 CreateUser () {stringClassnamespace ="Fanshelk.class1"; ObjectObjType =Createobjectnocache (AssemblyPath, classnamespace); return(ICLASS1) ObjType; } }}
DATABLL's Code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingDatademo;usingIfanshelk;namespacedatabll{ Public classClass1 { Public ReadOnlyIClass1 A =Dataaccess.createuser (); PublicClass1 () {} Public stringTest () {return "Success"; } }}
Implementation of MVC's HomeController.cs call
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingDATABLL;namespacemvctest.controllers{ Public classHomecontroller:controller {// //GET:/home/ PublicActionResult Index () {Class1 a=NewClass1 (); A.test (); returnView (); } }}
Three, however, the most likely problem in implementing reflection is the failure to load the file or assembly "xxx (Fanshelk)" or one of its dependencies. The system cannot find the file specified.
This is due to the resulting assembly.load (assemblypath) in the reflection. CreateInstance (Classnamespace); Assembly.Load (AssemblyPath) This finds a class library that does not reflect
PS: It is time to check if you load DLL path error, that is, DLL file exists, but the load path is incorrect
But before you know this:Assembly.Load () the DLL path to load is exactly where!!!!!!!!!!
According to the question, there are the following guesses:
1 "Under the \bin\debug of the Reflection Method class library? (ie the datademo\bin\debug of my Project)
2 "Under the \bin\debug of the MVC site? (ie the mvctest\bin\debug of my Project)
3 "Under the \bin\debug of the reflection implementation class library? (ie the databll\bin\debug of my Project)
Resolved as follows:
1 "In the Reflection Method Class Library (Datademo), add a reference to reflect the class library (Fanshelk), which will generate FanSheLK.dll to the datademo\bin\debug of the project, but experimentally proves that this is wrong, As a hint: failed to load file or assembly "xxx (Fanshelk)" or one of its dependencies. The system cannot find the file specified
2 "Add a reference to the MVC site, add a reference to reflect the class library (Fanshelk), the generated FanSheLK.dll to the project \bin\debug, the magic results appear, the method Assembly.Load successfully loaded, but still need to continue the experiment ....
3 "In the Reflection Implementation class Library (DATABLL), add the reference, add the reference to reflect the class library (Fanshelk), the generated FanSheLK.dll to the \bin\debug of the project, the magic results appear again, method Assembly.Load successfully loaded the
There are new questions: Why are there two assumptions that are successful? Depending on the two cases cited separately, we look at them in the same place and not the same place
1 "The reflection implementation of the Class library (DATABLL) and the MVC site of the project \bin\debug under the DLL are deleted
2. Add a reference to the MVC site, add a reference to reflect the class library (Fanshelk), rebuild the project, and we will find that FanSheLK.dll is generated under \bin\debug in the MVC site, and the Reflection Implementation Class Library (DATABLL) does not
3 "in Reflection implementation class Library (DATABLL), add Reference, add Reference to Reflection Class library (Fanshelk), Project rebuild, at this time we will find FanSheLK.dll in the MVC site \bin\ Generated under Debug and Reflection Implementation Class Library (DATABLL)
4 "After the comparison we found common ground: FanSheLK.dll will be in the MVC site \bin\debug under the generation, then we delete him, test
5 "concludes that Assembly.Load () is looking for an address that is under the \bin\debug of the MVC site, if reflection appears: failed to load file or assembly" xxx (Fanshelk) "or one of its dependencies. The system cannot find the file specified. Error: We should see if there is a DLL for the Reflection class library in this directory
PS: Why do you implement class libraries (DATABLL) in reflection, add references, add references to reflect class libraries (FANSHELK), Project rebuilds, FanSheLK.dll at the same time under \bin\debug of the MVC site and the Reflection Implementation Class library (DATABLL)?
The reason is that my databll is referenced by the MVC site, so will generate DATABLL reference DLL, if it is separated from the Multilayer class library (such as: Site reference a library, a library Reference b library, b Library Reference C library, The site generates A.DLL and B.DLL without generating C.DLL), but this does not occur in the class library (e.g. a library references B libraries, a library compilation does not compile references to B libraries)
Implementation of C # reflection