Dynamic (C #) dynamic invocation of class members using reflection
uses reflection to invoke a class member dynamically, requiring a method of the type class: InvokeMember. The declaration of the method is as follows (excerpt from MSDN):
public Object InvokeMember (
string name,
BindingFlags invokeattr,
Binder Binder,
object Target,
object[] args
);
parameter
name
String that contains the name of the constructor, method, property, or field member to invoke.
-OR-
an empty string ("") to indicate that the default member is invoked.
invokeattr
a bit mask, consisting of one or more BindingFlags that specify how the search executes. Access can be one of the BindingFlags, such as public, nonpublic, Private, InvokeMethod, and GetField. You do not need to specify a lookup type. If the lookup type is omitted, the BindingFlags.Public is applied | BindingFlags.Instance.
Binder
a Binder object that defines a set of properties and enables binding, which may involve the selection of overloaded methods, coercion of parameter types, and invocation of members by reflection.
-Or-
if it is a null reference (Nothing in Visual Basic), use DefaultBinder.
Target
the Object on which to invoke the specified member.
args
contains an array of arguments passed to the member to be invoked.
return value
the Object that represents the return value of the invoked member.
Note:
The following BindingFlags filter flags can be used to define the members included in the search:
in order to get the return value, you must specify BindingFlags.Instance or BindingFlags.Static.
Specifies that BindingFlags.Public can include public members in the search.
Specifies that BindingFlags.NonPublic can include non-public members (that is, private and protected members) in the search.
Specifies that bindingflags.flattenhierarchy can contain static members on the hierarchy.
The following BindingFlags modifier flags can be used to change how the search is executed:
Bindingflags.ignorecase, which indicates that the case of name is ignored.
BindingFlags.DeclaredOnly searches only the members declared on the Type, not the members that are simply inherited.
can use the following BindingFlags invocation flags to indicate the action to take on a member:
CreateInstance, which represents the call constructor. Ignore name. Invalid for other invocation flags.
InvokeMethod, which means calling a method without invoking a constructor or type initializer. Invalid for SetField or SetProperty.
GetField, representing the Get field value. Invalid for SetField.
SetField, which indicates setting the field value. Invalid for GetField.
GetProperty, representing the Get property. Invalid for SetProperty.
SetProperty represents setting properties. Invalid for GetProperty.
below through the example to the simple application of this method (I always thought, to let the example to make it easier to understand the meaning and role of text, the more simple examples of writing the more intuitive the better. )
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 a)
{
return A;
}
public void Display (string str)
{
Console.WriteLine (str);
}
}
///<summary>
Summary description of the
///Class1.
///</summary>
class Class1
{
///<summary>
///The 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);
//Calls methods with no return value
Type. InvokeMember ("Display", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, NULL, obj, new object[]{"Aldfjdlf"});
//Invoke method with return value
int i = (int) type. InvokeMember ("GetInt", BindingFlags.InvokeMethod | BindingFlags.Public | Bindingflags.instance,null,obj,new object[]{1});
Console.WriteLine (i);
//Set property value
type. InvokeMember ("Name", Bindingflags.setproperty,null,obj,new string[]{"abc"});