To use reflection to dynamically call class members, you must Type Class: Invokemember. The statement for this method is as follows (Excerpted from msdn ):
Public object invokemember (
String name,
Bindingflags invokeattr,
Binder binder,
Object target,
Object [] ARGs
);
Parameters
Name
String, which contains the name of the constructor, method, attribute, or field member to be called.
-Or-
An empty string ("") indicates the default Member of the call.
Invokeattr
One-bit blocking is composed of one or more bindingflags that specify the search execution method. Access can be one of bindingflags, such as public, nonpublic, Private, invokemethod, and getfield. You do not need to specify the search type. If the search type is omitted, bindingflags. Public | bindingflags. instance will be applied.
Binder
A binder object that defines a set of attributes and enables binding. Binding may involve selecting overload methods, mandatory parameter types, and calling members through reflection.
-Or-
If it is a null reference (nothing in Visual Basic), defaultbinder is used.
Target
The object on which the specified Member is to be called.
ARGs
An array containing parameters passed to the member to be called.
Return Value
Indicates the object returned by the called member.
Note:
The following bindingflags filter flag can be used to define members included in the search:
To obtain the returned value, bindingflags. instance or bindingflags. Static must be specified.
Specify bindingflags. Public to include public members in the search.
Specify bindingflags. nonpublic to include non-public members (Private Members and protected members) in the search ).
Specify bindingflags. flattenhierarchy to include static members in the hierarchy.
The following bindingflags modifier can be used to change the search execution method:
Bindingflags. ignorecase, indicating that the name case is ignored.
Bindingflags. declaredonly: searches for members declared on the type, rather than simply inherited members.
The following bindingflags call flag can be used to indicate the operations to be performed on members:
Createinstance, indicating to call the constructor. Ignore name. Invalid for other call flag.
Invokemethod indicates that a method is called, instead of a constructor or an initial value of the type. It is invalid for setfield or setproperty.
Getfield indicates obtaining the field value. It is invalid for setfield.
Setfield, indicating to set the field value. Invalid for getfield.
Getproperty indicates obtaining the property. It is invalid for setproperty.
Setproperty indicates setting properties. Invalid for getproperty.
The following example is a simple application of this method (I always thought that the simpler the example, the more intuitive the better, to make it easier to understand the meaning and function of the text .)
Code
UsingSystem;
UsingSystem. reflection;
NamespaceLeleapplication9
{
ClassLove
{
Public IntField1;
Private String_ Name;
PublicLove ()
{
}
Public String Name
{
Get
{
Return _ Name;
}
Set
{
_ Name = Value;
}
}
Public int getint ( int )
{< br> return A;
}
Public VoidDisplay (StringStr)
{
Console. writeline (STR );
}
}
Class Class1
{
[Stathread]
Static Void Main ( String [] ARGs)
{
Love = New Love ();
Type type = Love. GetType ();
Object OBJ = Type. invokemember ( Null ,
Bindingflags. declaredonly |
Bindingflags. Public | Bindingflags. nonpublic |
Bindingflags. Instance | Bindingflags. createinstance, Null , Null , ArgS );
// Call a method without a return value
Type. invokemember ( " Display " , Bindingflags. invokemethod | Bindingflags. Public | Bindingflags. instance, Null , OBJ, New Object [] { " Aldfjdlf " });
// Call methods with returned values
Int I = ( Int ) Type. invokemember ( " Getint " , Bindingflags. invokemethod | Bindingflags. Public | Bindingflags. instance, Null , OBJ, New Object [] { 1 });
Console. writeline (I );
// Set attribute values
Type. invokemember ( " Name " , Bindingflags. setproperty, Null , OBJ, New String [] { " ABC " });
// Get Attribute Value
String Str = ( String ) Type. invokemember ( " Name " , Bindingflags. getproperty, Null , OBJ, Null );
Console. writeline (STR );
// Set field value
Type. invokemember ( " Field1 " , Bindingflags. setfield, Null , OBJ, New Object [] { 444 });
// Get Field Value
Int F = ( Int ) Type. invokemember ( " Field1 " , Bindingflags. getfield, Null , OBJ, Null );
console. writeline (f);
console. readline ();
}< BR >}< br>