Dynamic (C #) dynamic invocation of class members with reflection
Invoking a class member dynamically using reflection requires 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
);
Parameters
Name
String that contains the name of the constructor, method, property, or field member to invoke.
Or
An empty string ("") that represents calling the default member.
Invokeattr
A bitmask 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
An array that contains the arguments that are passed to the member to be invoked.
return value
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 either 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, only the members declared on the Type are searched, not the members that are simply inherited.
You can use the following BindingFlags call flags to represent actions to be taken on a member:
CreateInstance, which represents calling the 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.
The following examples of this method for a simple application (I always thought, to let the example to make it easier to understand the meaning and role of the text, the more simple examples of writing the more intuitive the better.) )
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.