Overview
The reflection of the Assembly and the dynamic creation of the class object are commonly used in the automation programming to the knowledge principle, such as plug-in programming, template design patterns, can be used to dynamically create instantiated objects, the dynamic loading of classes. Here is a brief summary of the commonly used framework reflective knowledge points (generics and non-generics); the. NET Framework's three built-in classes to use reflection: System.Reflection.Assembly, System.Type, and System.activator;
- the System.Reflection.Assembly class describes the. NET assembly. In. NET, an assembly is a hive. For a typical Windows program, the assembly is configured as a single WIN32 executable with specific additional information to make it fit. NET runtime environment. Assemblies can also be configured as WIN32 DLLs (dynamic-link libraries), with the same needs. NET requires additional information. the System.Reflection.Assembly class can get information about the assembly at run time. This information includes the type information that the assembly contains.
- The System.Type class describes the type definition. A type declaration can be a class, an interface, an array, a struct, or an enumeration. After a class is loaded, the System.Type class can be used to enumerate the methods, properties, events, and interfaces that the class supports.
- The System.activator class is used to create an instance of a class.
Assembly ASSEMBLY
How assemblies are loaded and read
assembly[] assbycustmlist = System.AppDomain.CurrentDomain.GetAssemblies (); // gets the current all assemblies typeof(Iserviceplugin). Assembly; // The Assembly where the current type resides = Assembly.loadfile (filepath); // File path Loading
Assembly type
Get assembly type types
Assembly tmp = assembly.loadfile (file); // File path Loading type[] types = tmp. GetTypes (); // get all types of assemblies type[] items= tmp. Getexportedtypes (); // Get assembly Common type
To determine the property information of an assembly: interface implementation, generic type
Type t=GetType (); Type[] Interfaces= T.getinterfaces ();//gets the type upper layer implements all interfaces foreach(Type Theinterfaceinchinterfaces) { if(Theinterface.fullname = ="WinDemo.Core.Iplugin") {ret=true; Break; }}//t.gettypes (). Where (x = X.getinterfaces (). Count (p = = P.fullname = = "Pluginunittestproject.iserviceplugin") >0)//(abbreviated, function ibid.) type[] Interfaces= T.getinterface ("Iplugin");//gets the type upper layer implementation interface IPlugin type
Judging generic modes: Isgenerictype, GetGenericTypeDefinition (), and GetGenericArguments ()
PrivateIenumerable<type> gethandlertypes<t> ()whereT:command {varHandlers =typeof(icommandhandler<>). Assembly.getexportedtypes (). Where (x=x.getinterfaces (). Any (a= = A.isgenerictype && a.getgenerictypedefinition () = =typeof(icommandhandler<>) )) . Where (H=h.getinterfaces (). Any (ii=II. GetGenericArguments (). Any (AA=>aa==typeof(T))). ToList (); returnhandlers; }
The creation of the program
Through the CreateInstance of the Assembly
Assembly tmp = Assembly.loadfile (filepth); // file path loading type[] types = tmp. GetTypes (); BOOL false ; foreach inch types) { if (Isvalidplugin (t)) { = (iserviceplugin) tmp. CreateInstance (T.fullname);
Dynamic loading of assemblies inside methods, dynamic invocation
MethodInfo Medo = T.getmethod ("Invoketask"); Medo. Invoke (serple, NULL);
} }
by Activator.CreateInstance
NULL ; foreach inch typeitemlist) { new servicepluginattribute (); var serple = activator.createinstance (ITM);
Methods inside the dynamic invocation assembly
MethodInfo Medo = T.getmethod ("Invoketask"); Medo. Invoke (serple, null);
}
. NET Framework Reflection Summary