C # Reflection---Properties

Source: Internet
Author: User

C # Reflection detailed

(1) Reflection Get Properties

Let's take a look at some of the enumeration parameters that get the reflection properties: BindingFlags

instance| public: Get common instance properties (non-static) Instance| NonPublic: Gets the non-public instance property (non-static). (private/protect/internal) Static| public: Get common static propertiesstatic | NonPublic: Gets the non-public static property. (private/protect/internal) Instance| static| public: Get a common instance or static property instance| static| NonPublic: Non-fetching of public instances or static properties

Official explanation: 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 members 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 searches are performed:

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 indicate what action to take on a member:

A CreateInstance that represents the call constructor. Ignore name. Invalid for other call flags.

A InvokeMethod that represents the calling method without calling a constructor or type initializer. Invalid for SetField or SetProperty.

A GetField that represents the Get field value. Invalid for SetField.

A SetField that represents the Set field value. Invalid for GetField.

A GetProperty that represents the Get property. Invalid for SetProperty.

The SetProperty represents the Set property. Invalid for GetProperty.

BindingFlags.Instance: object instance
BindingFlags.Static: Static members
BindingFlags.Public: Refers to a public member that can be included in a search
BindingFlags.NonPublic: Refers to the inclusion of non-public members (that is, private members and protected members) in the search
BindingFlags.FlattenHierarchy: Refers to a static member that can contain a hierarchy
Bindingflags.ignorecase: Indicates ignoring the case of name
BindingFlags.DeclaredOnly: Searches only for members declared on Type without searching for members that are simply inherited
Bindingflags.createinstance: Represents the call constructor. Ignore name. Invalid for other call flags

Get the properties of the method directly from the Microsoft Source Library to get

 public   propertyinfo[] GetProperties ();//  get all the public properties in the property directly, including inherited public properties   public  abstract  Propertyinfo[] GetProperties (BindingFlags bindingattr);// Gets the corresponding property at the level of the attribute, such as obtaining a public private static property, etc. Includes inherited protected and internal 

Public PropertyInfo GetProperty (string name);//property name, case-sensitive
PropertyInfo GetProperty (string name, BindingFlags bindingattr); filter
Public BindingFlags by the specified name and PropertyInfo criteria GetProperty (string name, type returntype);//Gets the property, public property, by the type of the specified name and return value. If not, returns null
public PropertyInfo GetProperty (string name, type[] types), or an array of types with the specified name or attribute arguments, or new type[]{} if the property is an attribute Or new type[0], if it is an indexed property, the normal Type array, the name of the public property
Indexer with
"Item"
Public PropertyInfo GetProperty (string name, type ReturnType, type[] types)//Gets the property based on the specified name and return type and the type array of the parameter
Public PropertyInfo GetProperty (string name, Type returntype, type[] types, parametermodifier[] modifiers);// The default last parameter is NULL
Public PropertyInfo GetProperty (string name, BindingFlags bindingattr, Binder binder, Type returntype, type[] types, Param Etermodifier[] modifiers);//The default last parameter is NULL

Assign a value to a property after obtaining the attribute, the source code obtained from Microsoft

 PropertyInfo pi = t.getproperty ( " sex   "  = pi. GetValue (My,null ); Gets the value of the property 

Public virtual object GetValue (Object obj, Object[]//////////The value of the property, where index is a multi-parameter phenomenon for indexers, or null
Public abstract object GetValue (object obj, if not indexer) BindingFlags invokeattr, Binder binder, object[] index, CultureInfo culture);//Gets the value of the corresponding property

 Public Virtual void SetValue (objectObject Object [] index);//assigning property values to an instance object
public abstract void SetValue (Object obj, Object value, BindingFlags invokeattr, Binder binder, object[] index, Cultureinf o culture);

C # Reflection---Properties

Related Article

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.