What is the reflection
Reflection is an important mechanism in .NET, you can be obtained for each program or program type focus by reflection at runtime (including class, structure, commission and enumerations, etc.) information of members and member. With reflection, for each type can be well aware. In addition, I can also create objects directly, do not know even if the type of the object in the compilation.
Reflection purposes:
(1) Assembly definitions and loading assemblies, load modules listed examples, and this assembly in the assembly list search type and the type created.
(2) Module understanding module assembly comprising a module, and the like, can also get all global methods defined on the module, or other specific non-global method.
(3) An understanding of the use ConstructorInfo constructor parameters, access modifiers (e.g. pulic or private) and implementation details (such as the abstract, or virtual) and so on.
(4) The method of using the name of MethodInfo understanding, return type, parameters, access modifiers (e.g. pulic or private) and implementation details (such as the abstract, or virtual) and so on.
(5) using FiedInfo know the name of the field, the access modifier (e.g., public or private) and implementation details (e.g., static) and the like, and get or set field values.
(6) use the name EventInfo on the events, the event handler type data, custom attributes declared type and reflection type, etc., to add or remove an event handler.
(7) Use PropertyInfo know the name of the attribute, the data type, the declared type, the reflective type or writable and read-only status, get or set the property value.
(8) ParameterInfo know the name of the parameter, the data type, the parameter is an input or output parameter, and the parameter in the method signature location.
Reflection namespace used:
the System.Reflection
the System.Type
the System.Reflection.Assembly
reflector used in primary classes:
the System.Type class - can access any data type information given by this class.
System.Reflection.Assembly class - it can be used to access information for a given assembly, or the assembly to load the program.
System.Type categories:
System.Type class plays a central role in reflection. But it is an abstract base class, Type corresponding to each data type has the derived class, the derived class objects that we use the methods, fields, properties, to find all information about the type.
Get the type of the given reference Type There are three common ways:
● using C # typeof operator.
T = typeof type (String);
● objects using the GetType () method.
S = String "grayworm";
the Type s.GetType = T ();
● may also call the static method GetType Type class ().
Type t = Type.GetType ( "System.String"
above these three codes are acquired string type Type, after removal of the string type Type references t, we can detect a structure of type string by t.
string n = "grayworm";
Type t = n.GetType();
foreach (MemberInfo mi in t.GetMembers())
{
Console.WriteLine("{0}/t{1}",mi.MemberType,mi.Name);
}
Type class attributes :
the Name Data Type name
fully qualified name FullName data type (including namespace name)
the Namespace-defined data type namespace name
IsAbstract indicating whether the type is an abstract type
IsArray indicates whether the type is an array
IsClass indicating whether the type is a class
IsEnum indicates whether the type is an enumeration
IsInterface indicating whether the type is an interface
IsPublic indicating whether the type is public
IsSealed indicating whether the type is sealed class
IsValueType indicating whether the type is a value type of
the method class type:
GetConstructor (), GetConstructors (): returns the ConstructorInfo type, the class constructor for obtaining information
GetEvent (), GetEvents (): returns the EventInfo type used to obtain information about the events of that class
GetField (), GetFields (): returns the type FieldInfo for acquiring field class (member variables) information
GetInterface (), GetInterfaces (): returns the InterfaceInfo type used to obtain an interface implemented by this class of information
GetMember (), GetMembers (): returns the MemberInfo type used to obtain information for all members of the class
GetMethod (), GetMethods (): Returns the MethodInfo type, method of obtaining information for the class
GetProperty (), GetProperties () : PropertyInfo return type for the information acquisition properties of the class
can call these members, which is the way of calling the InvokeMember type () method, or calls the MethodInfo, and other classes PropertyInfo the invoke () method.
View constructor class:
NewClassw nc = new NewClassw();
Type t = nc.GetType();
ConstructorInfo[] ci = t.GetConstructors(); //获取类的所有构造函数
foreach (ConstructorInfo c in ci) //遍历每一个构造函数
{
ParameterInfo[] ps = c.GetParameters(); //取出每个构造函数的所有参数
foreach (ParameterInfo pi in ps) //遍历并打印所该构造函数的所有参数
{
Console.Write(pi.ParameterType.ToString()+" "+pi.Name+",");
}
Console.WriteLine();
}
Dynamically generated object using a constructor function:
Type t = typeof(NewClassw);
Type[] pt = new Type[2];
pt[0] = typeof(string);
pt[1] = typeof(string);
//根据参数类型获取构造函数
ConstructorInfo ci = t.GetConstructor(pt);
//构造Object数组,作为构造函数的输入参数
object[] obj = new object[2]{"grayworm","hi.baidu.com/grayworm"};
//调用构造函数生成对象
object o = ci.Invoke(obj);
//调用生成的对象的方法测试是否对象生成成功
//((NewClassw)o).show();
Generating object with Activator:
Type t = typeof(NewClassw);
//构造函数的参数
object[] obj = new object[2] { "grayworm", "hi.baidu.com/grayworm" };
//用Activator的CreateInstance静态方法,生成新对象
object o = Activator.CreateInstance(t,"grayworm","hi.baidu.com/grayworm");
//((NewClassw)o).show();
View class attributes:
NewClassw nc = new NewClassw();
Type t = nc.GetType();
PropertyInfo[] pis = t.GetProperties();
foreach(PropertyInfo pi in pis)
{
Console.WriteLine(pi.Name);
}
View public methods in the class:
NewClassw nc = new NewClassw();
Type t = nc.GetType();
MethodInfo[] mis = t.GetMethods();
foreach (MethodInfo mi in mis)
{
Console.WriteLine(mi.ReturnType+" "+mi.Name);
}
View class public fields
NewClassw nc = new NewClassw();
Type t = nc.GetType();
FieldInfo[] fis = t.GetFields();
foreach (FieldInfo fi in fis)
{
Console.WriteLine(fi.Name);
} (http://hi.
Generating reflection object and invoke property methods and operations fields,
NewClassw nc = new NewClassw();
Type t = nc.GetType();
object obj = Activator.CreateInstance(t);
//取得ID字段
FieldInfo fi = t.GetField("ID");
//给ID字段赋值
fi.SetValue(obj, "k001");
//取得MyName属性
PropertyInfo pi1 = t.GetProperty("MyName");
//给MyName属性赋值
pi1.SetValue(obj, "grayworm", null);
PropertyInfo pi2 = t.GetProperty("MyInfo");
pi2.SetValue(obj, "hi.baidu.com/grayworm", null);
//取得show方法
MethodInfo mi = t.GetMethod("show");
//调用show方法
mi.Invoke(obj, null);
System.Reflection.Assembly class
information can be obtained assembly Assembly class, you can also dynamically load an assembly, and to find the type of information in a centralized program, and create an instance of that type.
Use Assembly class can reduce the coupling between the assemblies, it is conducive to the rationalization of a software configuration.
Back through the assembly name Assembly object
Assembly ass = Assembly.Load ( "ClassLibrary831"
return Assembly object by DLL file name
Assembly ass = Assembly.LoadFrom ( "ClassLibrary831.dll"
Get assembly Classes Assembly
the Type T = ass.GetType ( "ClassLibrary831.NewClass"); // parameter must be the full name of the class
acquisition program by centralizing all classes assembly
type [] t = ass.GetTypes ()
; reflected by the name of the assembly
Assembly ass = Assembly.Load("ClassLibrary831");
Type t = ass.GetType("ClassLibrary831.NewClass");
object o = Activator.CreateInstance(t, "grayworm", "http://hi.baidu.com/grayworm");
MethodInfo mi = t.GetMethod("show");
mi.Invoke(o, null);
Full name which reflects all types of DLL files by
Assembly assembly = Assembly.LoadFrom("xxx.dll的路径");
Type[] aa = a.GetTypes();
foreach(Type t in aa)
{
if(t.FullName == "a.b.c")
{
object o = Activator.CreateInstance(t);
}
}