To use reflection to dynamically call class members, you need a method of the 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 .)
Using System;
Using System. Reflection;
Namespace ConsoleApplication9
{
Class Love
{
Public int field1;
Private string _ name;
Public Love ()
{
}
Public string Name
{
Get
{
Return _ name;
}
Set
{
_ Name = value;
}
}
Public int GetInt (int)
{
Return;
}
Public void Display (string str)
{
Console. WriteLine (str );
}
}
/// <Summary>
/// Summary of Class1.
/// </Summary>
Class Class1
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main (string [] args)
{
//
// TODO: Add code here to start the application
//
Love 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 a method with a returned value
Int I = (int) type. InvokeMember ("GetInt", BindingFlags. InvokeMethod | BindingFlags. Public | BindingFlags. Instance, null, obj, new object [] {1 });
Console. WriteLine (I );
// Set the attribute value
Type. InvokeMember ("Name", BindingFlags. SetProperty, null, obj, new string [] {"abc "});
// Obtain the property value
String str = (string) type. InvokeMember ("Name", BindingFlags. GetProperty, null, obj, null );
Console. WriteLine (str );
// Set the field value
Type. InvokeMember ("field1", BindingFlags. SetField, null, obj, new object [] {444 });
&