Two forms of information about the method
Reflection is a C# function that allows users to obtain class information. The Type object maps the underlying object it represents;
In .Net, once the Type object is obtained, you can use the GetMethods() method to obtain a list of methods supported by this type; two forms of this method:
MethodInfo [] GetMethods()
MethodInfo [] GetMethods(BindingFlags bindingflas) : Its parameters have some limitations BindingFlags is an enum
Enumerate members [DeclaredOnly,Instance,Public] Use of the function of enumerate members. After using the "." symbol in the compiler, observe carefully [I believe you will understand it soon]
ParameterInfo[] GetParameters() method returns a method's parameter list
The class MyClass used below, I folded it for the sake of easy reading!
class MyClass
{
int x;
int y;
public MyClass(int i, int j)
{
= i;
= j;
}
public int Sum()
{
return x + y;
}
public bool IsBetween(int i)
{
if (x < i && i < y)
{
return true;
}
return false;
}
public void Set(int a, int b)
{
x = a;
y = b;
}
public void Set(double a, double b)
{
x = (int)a;
y = (int)b;
}
public void Show()
{
("x: " + x + " y: " + y);
}
}
MyClass
Main:
Type t = typeof(MyClass);//Get a Type object representing the MyClass class
("Get the name of the current member" + );
("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
("Supported Method");
#region First form
//MethodInfo[] mi = ();//Show supported methods in Class class
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Methods GetMethods() displays the base class object method of MyClass
//Let’s talk about another form of GetMethods(), with limited display
#endregion
#region The second form
MethodInfo[] mi = ( | | );
#endregion
foreach (MethodInfo m in mi)
{
//After returning, print out the type of the member in the MyClass class (the return value type of the method) and the extremely method name
(" " + + " " + + " (");//ReturnType gets the return type of this method
ParameterInfo[] pi = ();//Get the parameters of the method
for (int i = 0; i < ; i++)
{
(pi[i]. + " " + pi[i].Name);//ParameterType Gets the Type (type) of this parameter
if (i+1<)
{
(", ");
}
}
(")");
();
}
();
Calling methods using reflection
The above discusses how to obtain the supported methods of a type, but we have made sufficient preparations for obtaining calls to methods!
The Invoke() method in the MethodInfo class provides this skill!
One form of it: object Invoke(object object,obj,object [] paraments)
obj is an object reference that will call the method on the object it points to. For static methods, obj must be null.
All parameters that need to be passed to the method must be specified in the parameters array. If the method does not require parameters, the parameters must be null
The Invoke() method of the base class MethodBase returns the return value of the called method
Please see the following examples:
The MyClass class Set() method has changed:
public void Set(int a, int b)
{
("Set(int,int)");
x = a;
y = b;
Show();
}
public void Set(double a, double b)
{
("Set(double,double)");
x = (int)a;
y = (int)b;
Show();
}
Type t = typeof(MyClass);
MyClass reflectOb = new MyClass(10, 20);
int val;
("Invoke methods in " + );//Calling the method of the MyClass class
();
MethodInfo[] mi = ();
foreach (MethodInfo m in mi)//Calling each method
{
//Get method parameters
ParameterInfo[] pi = ();
if (("Set",)&&pi[0].ParameterType==typeof(int))
{
// Specify (,) and ()
// The region, case and collation rules to be used for some overloading of the method.
// Compare strings using sequence number sorting rules
object[] obj = new object[2];
obj[0] = 9;
obj[1] = 18;
(reflectOb, obj);
}
else if (("Set",)&&pi[0].ParameterType==typeof(double))
{
object[] obj = new object[2];
obj[0] = 1.12;
obj[1] = 23.4;
(reflectOb, obj);
}
else if (("Sum",))
{
val = (int)(reflectOb, null);
("Sum is : " + val);
}
else if (("IsBetween", ))
{
object[] obj = new object[1];
obj[0] = 14;
if ((bool)(reflectOb, obj))
{
("14 is between x and y");
}
}
else if (("Show",))
{
(reflectOb,null);
}
}
Main