Dynamic Call of class members using reflection

Source: Internet
Author: User
Tags constructor reflection
(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 (
String name,
BindingFlags invokeAttr,
Binder binder,
Object target,
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.

Return value
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.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

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.