Reflection (Reflection)
Reflection is a mechanism that is provided by the. NET Framework class library as a mechanism to display metadata in an assembly. To take advantage of reflection, you first need to get the type instance, which represents any type of information that provides a set of properties and methods to get the metadata of a type, can get member information for a class and set or modify property members, invoke member methods, if appropriate code access security Access Security permissions, reflection can also bypass accessibility rules, such as accessing private, protected members
Get type Object
There are two ways to get the type representation of a kind, an object of any type calls the GetType () method or uses the TypeOf operator Animal a = new Animal ();
Type typeForAnimal1=a. GetType ();
Type typeForAnimal2 = typeof ( Animal );
bool Test = TypeForAnimal1 = = typeForAnimal2;
Assembly Class (System.Reflection.Assembly)
Represents a description of an assembly, which makes it easy to load an assembly into the current application domain to obtain information about that assembly
//Properties
CodeBase
//Returns a string representing the actual location of the previously specified assembly
entrypoint
//Returns a MethodInfo instance representing the portal of the currently loaded assembly
Evidence
//Returns an evidence instance that contains information about the signature and the initial location of the code
FullName
//Returns a String that represents the name, version number, and cultural identity of the currently loaded assembly
GlobalAssemblyCache
//Returns a BOOL value indicating whether the currently loaded assembly is loaded from the global assembly cache (the global assembly cache is used to store assemblies that are shared by multiple applications)
Location
//Returns a String that represents the path of the currently loaded assembly//static method
Createqualifiedname ()
//Create the name of the type qualified by the display name of the assembly that contains the type
//Assuming that the tree is defined in a Forest assembly, the method returns Tree.forest
getassembly
//Returns an acssemly instance that is a generated object based on the type supplied by the parameter
The Type class (System.Reflection. Type)
Gets the type representation of any kind.
//GetMethod, GetMethods, GetProperty, GetProperties methods with BindingFlags parameters to specify which members need to be searched
the possible values for the//bindingflags enumeration are as follows
//1.public: Get public Members only
//2.nopublic: Get non-public members only
//3.instance: Gets the members of the object only
//4.static: Gets the members of the class
//5.declaredonly: Do not get inherited members
//bindingflags Enumeration must initializes 1 or item 2 to combine other items, otherwise it will only get null
GetMethods ()
//Returns a MethodInfo array that stores information about the method being searched for
///Example:
//Get only the public instance method of the class, not including inheritance
Type t = typeof ( Animal );
MethodInfo [] Methods = t. GetMethods ( BindingFlags. Public | BindingFlags . Instance | BindingFlags . DECLAREDONLY);
GetProperties ()
//Returns a PropertyInfo array that stores information about the property being searched for
GetMethod ( string methodName)
//Gets information about a single method of the type, returns a MethodInfo object
GetProperty ( string PropertyName)
//Gets information about a single property of the type, returns a PropertyInfo object
Isgenerictype
//Determines whether the current type object is a generic or generic method
///Example:
Type Type = typeof ( Animal<string>);
Console. Write (Type. Isgenerictype); //print True
ContainsGenericParameters
//Determines whether the type parameter of a type or method is not yet an argument
///Example:
Type Type = typeof ( Animal<>);
Console. Write (Type. ContainsGenericParameters); //print True
type type = typeof ( Animal<string>);
Console. Write (Type. ContainsGenericParameters); //print false
getgenericarguments ()
//Gets the type parameter of the generic, generic method
///Example:
Animal<string> A = new Animal<string > {};
type type = typeof ( Animal<string>);
type [] animalgenerictypes = type. GetGenericArguments ();
foreach(Type animalgenerictype in Animalgenerictypes)
{
Console. WriteLine (Animalgenerictype. GetProperties ());
}
MethodInfo class (System.Reflection. MethodInfo)
Information that represents the method
Name
//Method name
returntype. Name
//Gets the name of the return type of the method
Invoke ()
//Execute the method
getparameters ()
//Gets the parameter information of the method, returns a ParameterInfo array to store all the formal parameter information
///Example:
//Read all methods of a class
Type x = typeof (MyClass);
MethodInfo [] Methods = x. GetMethods ();
StringBuilder s = new StringBuilder ();
foreach ( var method in methods)
{
s. AppendFormat ( "<div style= ' margin-top:100px; ><div> function name:{0}</div> " , method. Name);
s. AppendFormat ( "<div> function return type:{0}</div>" , Method. ) ReturnType. Name);
foreach ( var @params in method. GetParameters ())
{
s. AppendFormat ( "<div style= ' margin-left:100px; > function parameter name:{0}</div> " , @params. Name);
s. AppendFormat ( "<div style= ' margin-left:100px; > function parameter Type:{0}</div> " , @params. ParameterType. Name);
}
s. Append ( "</div>" );
}
Console. WriteLine (S. ToString ());
ParameterInfo class (system.reflection. ParameterInfo)
Represents the parameter information for a method.
Name
//Parameter name
parametertype. Name
//parameter type name
PropertyInfo class (system.reflection. PropertyInfo)
Represents the information for a property.
PropertyType
//Gets the type of the property
///Example:
PropertyInfo Property = typeof ( Animal ). GetProperties () [ 0 ];
bool isbool=Property. PropertyType = = typeof ( bool ); //Simple Type judgment so write
bool isclass = Property. PropertyType. IsClass; //Complex type judgment so write
GetValue (obj)
//Gets the property value of the specified object
SetValue (obj)
//Sets the property value of the specified object
///Example:
type type = typeof ( Animal );
PropertyInfo Property = type. GetProperty ( "Name" );
Animal t = new Animal ();
string name = Property. GetValue (t). ToString ();
Console. WriteLine (name);
C #-Learning Total Directory
C #-Reflection