Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
(C #) use reflection to dynamically call class members
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 (
Object  args
String, which contains the name of the constructor, method, attribute, or field member to be called.
An empty string ("") indicates the default member of the call.
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.
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.
If it is a null reference (Nothing in Visual Basic), DefaultBinder is used.
The Object on which the specified member is to be called.
An array containing parameters passed to the member to be called.
Indicates the Object returned by the called member.
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.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to firstname.lastname@example.org. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.