Dynamic Call of class members using reflection

Source: Internet
Author: User
Tags 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
);
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.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.