Reflection reflection needs to refer to using System.Reflection; namespace.
Through the Assembly class of Load (); Load the specified assembly
Assembly is not instantiated, so it can load () by calling the static method, loading the specified assembly.
By the type of Getcond in GetType () Refliuyl
?
| 1 2 3 |
Assembly assembly = Assembly.Load("RefLiuyl"); //获取类型,参数(名称空间+类) Type type = assembly.GetType("RefLiuyl.GetCon"); |
There is also a getname<t> () when using generics. Gets the type of the TypeOf directly (T);
Let's take a look at the partial internal load prototype static method:
// Assembly Internal Load prototype-part Public class Assembly { //load static reload prototype Public Static Assembly Load (AssemblyName assemblyref); Public Static Assembly Load (string assemblystring); }
The AssemblyName here is actually fetching the assembly.
New AssemblyName ("classlibrary"= Assembly.Load (classlibrary);
Assembly can also load an assembly through a path
Assembly Assem = Assembly.LoadFrom ("path");
// Assembly Internal Load prototype-part Public class Assembly { // Public Static Assembly LoadFrom (string path);
Two. Instance of a constructed type
?
| 1 2 3 |
Assembly assembly = Assembly.Load("RefLiuyl"); //获取类型,参数(名称空间+类) Type type = assembly.GetType("RefLiuyl.GetCon");<br>获取到类型后,我们需要实例化它。<br><br>通过System.Activator的CreateInstance方法来实例化它。 |
AssemblyName classlibrary =NewAssemblyName ("classlibrary"); //Assembly Assembly = Assembly.Load (classlibrary);Assembly Assembly = Assembly.Load ("classlibrary"); //get type by GetType ()//get type, parameter (Namespace + Class)Type type = assembly. GetType ("classlibrary.student"); //create an instance of the object, object type, parameter (Namespace + Class) ObjectInstance = assembly. CreateInstance ("classlibrary.student"); //or use the type directly ObjectInstance1 =activator.createinstance (type); //when using genericsType type =typeof(T); T Destobj= Activator.createinstance<t> ();
Activator.CreateInstance (); Returned is the class instance
GetProperties (); Returns all properties in the assembly type
// get Type by GetType () // get type, parameter (Namespace + Class) Type type = assembly. GetType ("classlibrary.student"); foreach inch type. GetProperties ()) { string namses= Prop. Name; }
The property fields of the instance class in the assembly are returned.