C # mainly supports 5 ways to dynamically create objects: 1. Type.InvokeMember 2. Contructorinfo.invoke 3. Activator.CreateInstance (Type) 4. Activator.CreateInstance (AssemblyName, TypeName) 5. Assembly.createinstance (TypeName) is the quickest way to 3, with the difference of Direct Create within an order of magnitude, about 7 times times slower than the level. Other ways, at least 40 times times more, the slowest is mode 4, which is slower by three orders of magnitude. Try new, less reflective
Assembly.createinstance () The difference between creating an instance and new is one: New is called by the Newobj method to instantiate the object directly on the managed heap to allocate memory, compile-time determined, and compile-time with type checking. The CreateInstance method creates an object by invoking the metadata of the assembly, according to the metadata description, and the type is unsafe, but it can create objects dynamically. The advantage should be flexibility. Difference two: Assembly.createinstance () is a dynamic load, that is, the runtime creates a class and does not validate that the class exists at compile time. New is statically loaded. The compile-time class has been compiled.
Assembly.Load ("assembly name"). CreateInstance ("namespace. Class") See: Http://www.tuicool.com/articles/bIjUzu
classProgram {Static voidMain (string[] args) {Ilovemm mm= (ILOVEMM) assembly.load ("Reflection"). CreateInstance ("Reflection.lovemm");//This is a direct coercion type conversion.mm. Output (); } } InterfaceIlovemm {voidOutput (); } classLovemm:ilovemm { Public voidOutput () {Console.WriteLine ("I Love you MM"); } }
About the difference between assembly.createinstance () and Activator.CreateInstance () methods in reflection MSDN, two methods are found: Assembly.createinstance method (String Use a case-sensitive search to find the specified type from this assembly, and then use the system activator to create an instance of it. The Activator.CreateInstance method (type) Creates an instance of the specified type using the constructor with the highest degree of matching to the specified parameter.
Create an object instance from an assembly string path = system.configuration.configurationsettings.appsettings["DAL"];//the assembly name of the data layer return ( Idbobject) Assembly.Load (path). CreateInstance (path+ ". DBObject ");
If your data tier is not a separate assembly, you can load it using the following method
// To create an object instance from an assembly string Path = system.configuration.configurationsettings.appsettings["DAL"]; // the assembly name of the data tier return (Idbobject) Assembly.Load (Path). CreateInstance (path+". DBObject");
If your data tier is not a separate assembly, you can load it in the following ways:
// creates an instance of the specified type using the constructor that is the most matched to the specified parameter string Path = system.configuration.configurationsettings.appsettings["DAL" ]; string typename=path+". DBObject"= Type.GetType (TypeName,true); return (Idbobject) Activator.CreateInstance (ObjType);
See: http://blog.163.com/jiang_tao_2010/blog/static/1211268902009817324945/
Own code://assembly Assembly = Assembly.Load (Websiteshopcode); var objgrab = (igrab) assembly. CreateInstance (Websiteshopcode + ". Grab ");
Type ObjType = Type.GetType (Websiteshopcode + ". Grab ", true); var objgrab = (igrab) activator.createinstance (objType);; var product = Objgrab.getproinfo (Createdtime, Websiteshopcode, I, URL);
Creating objects Dynamically