Learn and pay attention to the content of the C reflection example

  • 2020-05-19 04:35:45
  • OfStack

The first step in getting started with C# reflection is to understand that C# reflection provides encapsulation assemblies, modules, types of objects, and so on. You can then use reflection to dynamically create instances of a type, bind the type to an existing object, or get the type from an existing object and call its methods or access its fields and properties. If properties are used in your code, they can be accessed using reflection.

MSDN description:
Reflection usually has the following USES:
Use Assembly to define and load the assembly, load the modules listed in the assembly manifest, and find a type from this assembly and create an instance of that type.
Use Module to discover the following information: the assembly that contains the module, the classes in the module, and so on. You can also get all the global methods or other specific non-global methods defined on the module.
Use ConstructorInfo to discover the name of the constructor, parameters, access modifiers (such as public or private), implementation details (such as abstract or virtual), and so on. Call a specific constructor using Type's GetConstructors or GetConstructor methods.
Use MethodInfo to discover the following information: method name, return type, parameters, access modifiers (such as public or private), and implementation details (such as abstract or virtual). Use Type's GetMethods or GetMethod methods to invoke specific methods.
Use FieldInfo to discover the name of the field, access modifiers (such as public or private), and implementation details (such as static). And gets or sets the field value.
Discover the following information using EventInfo: event name, event handler data type, custom property, declaration type, reflection type, etc. And add or remove event handlers.
Use PropertyInfo to discover the name, data type, declaration type, reflection type, read-only or writable state of the property, etc. And gets or sets the property value.
Use ParameterInfo to discover the name of the parameter, the data type, whether the parameter is an input parameter or an output parameter, and the location of the parameter in the method signature.
When you work in the reflective only context of an application domain, use CustomAttributeData to learn about custom properties. With CustomAttributeData, you can check properties without creating instances of them.
The System.Reflection.Emit namespace class provides a special form of reflection that allows you to generate types at run time.
Reflection can also be used to create an application called a type browser, which enables the user to select a type and then view information about the selected type.
Reflection has other USES. Language compilers such as JScript use reflection to construct symbol tables. The class in the System.Runtime.Serialization namespace USES reflection to access the data and determine which fields to persist. The classes in the System.Runtime.Remoting namespace use reflection indirectly through serialization.

For the simplest example of C# reflection, start by writing the class library as follows:
 
using System; 
namespace ReflectionTest 
{ 
public class WriteTest 
{ 
//public method with parametors 
public void WriteString(string s, int i) 
{ 
Console.WriteLine("WriteString:" + s + i.ToString()); 
} 
//static method with only one parametor 
public static void StaticWriteString(string s) 
{ 
Console.WriteLine("StaticWriteString:" + s); 
} 
//static method with no parametor 
public static void NoneParaWriteString() 
{ 
Console.WriteLine("NoParaWriteString"); 
} 
} 
} 

The csc /t: library ReflectTest.cs command is compiled using the command line to generate the ReflectTest.dll library file.
Then write the following program:
 
using System; 
using System.Reflection; 
class TestApp 
{ 
public static void Main() 
{ 
Assembly ass; 
Type type; 
Object obj; 
Object any = new Object(); 
ass = Assembly.LoadFile(@"D:\Source Code\00.C# Sudy\01.Reflection\01\ReflectTest.dll"); 
type = ass.GetType("ReflectionTest.WriteTest"); 
/*example1---------*/ 
MethodInfo method = type.GetMethod("WriteString"); 
string test = "test"; 
int i = 1; 
Object[] parametors = new Object[] { test, i }; 
obj = ass.CreateInstance("ReflectionTest.WriteTest"); 
method.Invoke(obj,//Instance object of the class need to be reflect 
parametors); 
//method.Invoke(any, parametors);//RuntimeError: class reference is wrong 
/*example2----------*/ 
method = type.GetMethod("StaticWriteString"); 
method.Invoke(null, new string[] { "test" }); 
method.Invoke(obj, new string[] { "test" }); 
method.Invoke(any, new string[] { "test" }); 
/*example3-----------*/ 
method = type.GetMethod("NoneParaWriteString"); 
method.Invoke(null, null); 
} 
} 

A few points to note when learning C# reflection:
1. Specify that the class library file must use the absolute path, not the relative path (which feels a bit unreasonable and inconvenient)
Line 2.19, the namespace and class name must be specified from 1
3. In example 1, you have to instantiate a class that reflects to reflect, because the method to be used is not a static method.
4. Since this method has two parameters, you can specify the parameters using this Object method or you can write method.Invoke (obj, new Object[] {"test", 1});
5. In example 2, the method we want to use is a static method, and Invoke ignores the first parameter, which means that nothing we write will be called, even if we randomly new an any or Object, which of course is not recommended. However, in example 1, if we use an instance of type non-1 as a parameter in Invoke, it will result in a runtime error.
6. The third example is an example of calling a static method with no arguments, in which case we don't need to specify either parameter, just null.

Say a question, if the call class is a static class, need to pay attention to a problem, sure we can think of a problem, a static class cannot be instantiated, at that time, 31 rows instantiation of a class of methods we don't need to, direct use of Invoke can be achieved, otherwise will be a runtime error, in the same way, the first parameter will be ignored, as long as we passed to the parameters.
So much for C# reflection and C# reflection instances. I hope it will be helpful for you to learn about C# reflection and C# reflection instances.

Related articles: