Preliminary Solution of C#Assembly Reflection

Reflection is the process of discovering and running an assembly. Reflection provides internal information about an assembly such as.exe and.dll. Reflection allows you to see information about its internal classes, methods, interfaces, structures, properties, and attributes.
The namespace class System.Reflection contains multiple common classes for reflection.
Lift a chestnut:

Assembly uses this class to load and manipulate an assembly and obtain information about the assembly itself
 EventInfo This class holds the given event information
 FieldInfo This class holds the given field information
 MethodInfo This class holds the given method information
 MemberInfo This class is a base class that defines several common behaviors of EventInfo, FieldInfo, MethodInfo, PropertyInfo 
Module This class allows you to access a given module in multiple assemblies
 ParameterInfo This class holds the given parameter information
 PropertyInfo This class holds the given property information

System.Reflection.Assembly Class

The Assembly class allows you to dynamically load assemblies and view information inside the assemblies, commonly known as Load ();
Lift a chestnut:

Assembly assembly = Assembly.Load("AssemblyTest");

Assembly's object CreateInstance(string) method allows you to reflect and create an object with parameter 0 as the class name.


Type is the most commonly used class, and you can get internal information about a class through Type, or you can create an object through reflection

Type t = typeof(Example);//	Using typeof();

//Get Type object from System.Object.GetType()
Example example=new Example();
Type type=example.GetType();

// Get Type object from System.Type.GetType()
//Parameter 1: Assembly Limit Name
//Parameter two: true raises an exception if no type is found; false returns null.Specifyingfalse
//Parameter 3: The case-insensitive search performed is true, and the case-sensitive search performed for typeName is false.
Type type=Type.GetType("MyAssembly.Example",false,true);

//The most common is to create objects using reflection in conjunction with Activator.
Assembly assembly= Assembly.Load("MyAssembly");
Type type=assembly.GetType("Example");
object obj=Activator.CreateInstance(type);

Reflection Method

Ways to find classes through System.Reflection.MethodInfo:

Type type=typeof(Example);    
MethodInfo[] listMethodInfo=type.GetMethods();    //Get Method Set
foreach(MethodInfo methodInfo in listMethodInfo)         
Cosole.WriteLine("Method name is "+methodInfo.Name); //Loop method name

Use the reflection method to execute the methods in the class:

Assembly assembly= Assembly.Load("MyAssembly");
Type type=assembly.GetType("Example");
object obj=Activator.CreateInstance(type);
MethodInfo methodInfo=type.GetMethod("Hello World");  //Get MethodInfo object from method name
methodInfo.Invoke(obj,null);  //Parameter 1 is of type object[], which represents the corresponding parameter of the Hello World method, and the input value is null for no parameter

Reflection Properties

Find the properties of a class through System.Reflection.PropertyInfo

Type type=typeof(Example);
PropertyInfo[] listPropertyInfo=type.GetProperties();   //Get Property Set
 foreach(PropertyInfo propertyInfo in listPropertyInfo)         
 Cosole.WriteLine("Property name is "+ propertyInfo.Name); //Loop Attribute Set

Reflection Field

Find the fields in the class through System.Reflection.FieldInfo

SetValue (object, object) and GetValue (object) because they are used very similar to reflection properties

 ActivatorTest activatorTest = new ActivatorTest(); 
 System.Reflection.FieldInfo fieldInfo = activatorTest.GetType().GetField("nn");
     Console.WriteLine(fieldInfo.Name+":"+ fieldInfo.GetValue(activatorTest));
     fieldInfo.SetValue(activatorTest, "123");

Reflection characteristics

The GetCustomAttributes(Type,bool) of System.Reflection.MemberInfo reflects the attributes within a class. The following example reflects all the attributes of a class

Type type=typeof("Example");   
object[] typeAttributes=type.GetCustomAttributes(false);       //Get the properties of the Example class   
foreach(object attribute in typeAttributes)        
 Console.WriteLine("Attributes description is "+attribute.ToString());
Thirty-five original articles were published. 6. Visits 9174
Private letter follow

Keywords: Attribute

Added by x_filed2k on Sun, 19 Jan 2020 03:49:13 +0200