C#Reflection detailed solution
(1)Reflection acquisition attribute
First, explain the meaning of some enumeration parameters obtained from the reflection property: BindingFlags
Instance|Public:Get common instance attributes (not static)Instance|NonPublic:Gets non public instance attributes (not static). (private/protect/internal) Static|Public:Get public static attributesStatic|NonPublic:Gets non public static attributes. (private/protect/internal) Instance|Static|Public:Get public instance or static attributesInstance|Static|NonPublic:No public instance or static attribute is obtained.
Official explanation: in order to get the return value, BindingFlags.Instance or BindingFlags.Static must be specified.
The specified BindingFlags.Public can include public members in the search.
Specify that BindingFlags. NonPublic can include non-public members (that is, private members and protected members) in the search.
The specified BindingFlags.FlattenHierarchy can contain static members on the hierarchy.
The following BindingFlags modifier flags can be used to change the execution mode of search:
BindingFlags.IgnoreCase,Indicates ignoring the case of name.
BindingFlags.DeclaredOnly,Search only members declared on Type instead of members who are simply inherited.
You can use the following BindingFlags calling flag to indicate the actions to be taken to the members:
CreateInstance,Represents the call constructor. Ignore name. Invalid for other call labels.
InvokeMethod,Represents the calling method instead of calling the constructor or type initializer. Invalid for SetField or SetProperty.
GetField,Represents the value of the field. Invalid for SetField.
SetField,Indicates setting field values. Invalid for GetField.
GetProperty,Represents the acquisition of attributes. Invalid for SetProperty.
SetProperty Indicates setting properties. Invalid for GetProperty.
BindingFlags.Instance : Object instance
BindingFlags.Static : Static member
BindingFlags.Public : Refers to the inclusion of public members in search.
BindingFlags.NonPublic : Refers to the inclusion of non public members in the search, that is, private members and protected members.
BindingFlags.FlattenHierarchy : A static member that can contain a hierarchy.
BindingFlags.IgnoreCase : Indicates ignoring the size of name.
BindingFlags.DeclaredOnly : Search only members declared on Type instead of members who are simply inherited.
BindingFlags.CreateInstance : Represents the call constructor. Ignore name. Invalid for other call labels
The way to get attributes is obtained directly from the Microsoft source code.
public PropertyInfo[] GetProperties();//Directly get all the public attributes in the attribute, including inherited public attributes.public abstract PropertyInfo[] GetProperties(BindingFlags bindingAttr);//Get attributes at the level of attributes, such as public private static attributes, and so on, including inherited protected and internal attributes
public PropertyInfo GetProperty(string name);//Gets the public attribute according to the attribute name, and distinguishes the case from the case.
public PropertyInfo GetProperty(string name, BindingFlags bindingAttr);//Filter according to the specified name and BindingFlags condition.
public PropertyInfo GetProperty(string name, Type returnType);//Gets the attribute, public attribute according to the Type type of the specified name and return value. If not, return to Null
public PropertyInfo GetProperty(string name, Type[] types);//Gets an array of types with the specified name or attribute parameter, using new Type []{} or new Type [0], and normal Type arrays, public properties, if indexed
The name of the indexer is used."Item"
public PropertyInfo GetProperty(string name, Type returnType, Type[] types);//Gets the attribute based on the specified name and return type and the Type type array of the parameter.
public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers);//The default last parameter is null.
public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers);//The default final parameter is Null.
After getting the attribute, assign the attribute to the source code obtained from Microsoft.
PropertyInfo pi = t.GetProperty("sex");//Getting the sex attribute of an instanceObject obj= pi.GetValue(my,null);Gets the value of this property
public virtual object GetValue(object obj, object[] index);///Gets the value of an attribute, where index is a multi parameter phenomenon for indexer, if not indexer, then Null.
public abstract object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture);//Get the value of the corresponding property
public virtual void SetValue(object obj, object value, object[] index);//Assign attribute values to an instance object
public abstract void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture);