public abstract class Binder
Object
Binder
mscorlib
Reflection
Performs custom overload resolution and argument coercion to bind a member when reflection is used to invoke a member of a Type.
Late binding is controlled by a customized binding interface through reflection. The Binder class is designed to provide this functionality. Binder objects are used in overload resolution and argument coercion for dynamic invocation of members at runtime.Access to information obtained from reflection is controlled at two levels: untrusted code and code with ReflectionPermission.
Untrusted code is code with no special level of trust (such as code downloaded from the Internet). Such code is allowed to invoke anything that it would have been able to invoke in an early bound way.
ReflectionPermission controls access to metadata through reflection. If this permission is granted to code, that code has access to all the types in its application domain, assembly, and module. It can access information about public, family, and private members of all types it has access to. Two primary capabilities are granted:
[Note: The term "reflection" refers to the ability to obtain information about a Object during runtime. The primary means through which this information is accessed is via the Type of the object. Reflection allows the programmatic discovery of a type's metadata. The information included in the metadata includes details about the assembly or module in which the type is defined as well as members of the type. Reflection uses this information to provide the following primary services:
- The ability to read the metadata for family and private members of any type.
- The ability to access peer classes in the module and peer modules in the assembly.
The primary users of these services are script engines, object viewers, compilers, and object persistence formatters.
- Access to type information at runtime.
- The ability to use this type information to create instances, invoke methods, and access data members of the type.
Through reflection, methods can be bound and invoked at runtime. If more than one member exists for a given member name, overload resolution determines which implementation of that method is invoked by the system. Coercion can occur when a parameter specified for a method call does not match the type specified for the parameter in the method signature. When possible, the binder converts the parameter (coerces it) to the type specified by the method signature. Coercion might not be possible depending on the types involved.
To bind to a method, field, or property, typically a list of probable candidates is obtained from the Type of a Object. That list is the passed to the appropriate method of a Binder instance. Based on the other parameters passed to that method, typically (although not necessarily) one of the members of the list is chosen, and an object that reflects that member is returned.
The system supplies a default binder that provides default binding rules. Because binding rules vary among programming languages, it is recommended that each programming language provide a custom implementation of Binder .
]
System.Reflection Namespace
Binder Constructors
Binder Methods
Binder.BindToField Method
Binder.BindToMethod Method
Binder.ChangeType Method
Binder.ReorderArgumentArray Method
Binder.SelectMethod Method
Binder.SelectProperty Method
protected Binder();
Constructs a new instance of the Binder class.
System.Reflection.Binder Class, System.Reflection Namespace
public abstract FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture);
Selects a field from the specified set of fields, based on the specified criteria.
- bindingAttr
- A bitwise combination of BindingFlags values that control the binding process. For requirements, see the Behaviors section.
- match
- An array of FieldInfo objects whose elements represent the set of fields that reflection has determined to be a possible match, typically because the fields have the correct member name.
- value
- An object of a type that is assignment-compatible with the type of the field being searched for. [Note: For example, if value is an instance of a class, the type of that instance can be assigned to the type of the field returned by this method. Fields in match that cannot be assigned to this value are eliminated from the search.]
- culture
- The only defined value for this parameter is
null
.
A FieldInfo instance that reflects the field that matches the specified criteria. It is not required that this instance be contained in match. If a suitable field is not found, returnsnull
.
[Behaviors: For the bindingAttr parameter, the caller is required to specify either System.Reflection.BindingFlags.Public or System.Reflection.BindingFlags.NonPublic, and either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static. If at least one value from each pair is not specified, this method is required to returnnull
.]
System.Reflection.Binder Class, System.Reflection Namespace
public abstract MethodBase BindToMethod(BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, ref object state);
Selects a method based on the specified criteria.
- bindingAttr
- A bitwise combination of BindingFlags values that control the binding process. For requirements, see the Behaviors section.
- match
- An array of MethodBase objects that represent the set of methods that Reflection has determined to be a possible match, typically because they have the correct member name.
- args
- An array of objects that represent the parameters passed in the method invocation. The types, values, and order of the elements of this array might be changed by this method to match the signature of the selected method.
- modifiers
- The only defined value for this parameter is
null
.
- culture
- The only defined value for this parameter is
null
.
- names
- A String array containing the names of methods to be searched.
- state
- A binder-provided Object that keeps track of parameter reordering. The state object is totally defined by the implementer of the Binder class. This object is
null
if the binder does not reorder the argument array of the bound method.
A MethodBase instance that reflects the method that matches to the specified criteria. It is not required that this instance be contained in match . If a suitable method is not found, returnsnull
.
If state is notnull
, the system invokes System.Reflection.Binder.ReorderArgumentArray(System.Object[]@,System.Object) after this method returns. [Note: This allows a caller to map the argument array of a method back to the original form if the order has been altered by System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,System.Reflection.MethodBase[],System.Object[]@,System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[],System.Object@) . This is useful ifByRef
arguments are in the argument array, because the caller can retrieve those arguments in their original order on return from this method. When arguments are passed by name (i.e., using named arguments), the binder reorders the argument array and that is what the caller sees. This method insures that the original order of the arguments is restored.]
[Behaviors: For the bindingAttr parameter, the caller is required to specify either System.Reflection.BindingFlags.Public or System.Reflection.BindingFlags.NonPublic, and either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static. If at least one value from each pair is not specified, this method is required to return
null
.The System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,System.Reflection.MethodBase[],System.Object[]@,System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[],System.Object@) method is permitted to change the order of the argument array of a method call only if the binder returns, via the state parameter, a non-null opaque object that records the original order of the argument array. If, on return from System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,System.Reflection.MethodBase[],System.Object[]@,System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[],System.Object@), state is not
null
, the system calls System.Reflection.Binder.ReorderArgumentArray(System.Object[]@,System.Object) .]
System.Reflection.Binder Class, System.Reflection Namespace
public abstract object ChangeType(object value, Type type, CultureInfo culture);
Converts the type of the specified object to the specified type.
- value
- The object to be converted to a new Type .
- type
- The Type to which value is converted.
- culture
- The only defined value for this parameter is
null
.
A new object of the type specified by type . The contents of this object are equal to those of value .
[Behaviors: As described above.]
[Overrides: Implement this method to change the type of a member of a parameter array. Typically, it is recommended that implementations of this method perform only widening conversions.]
[Usage: This method is used to change the type of a element in a parameter array to match the type required by the signature of a bound method.]
System.Reflection.Binder Class, System.Reflection Namespace
public abstract void ReorderArgumentArray(ref object[] args, object state);
Restores the specified set of parameters to their original order after a call to System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,System.Reflection.MethodBase[],System.Object[]@,System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[],System.Object@) .
- args
- An array of objects whose elements represent the parameters passed to the bound method in their original order.
- state
- A binder-provided opaque object that keeps track of parameter reordering. This object is the same object that was passed as the state parameter in the invocation of System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,System.Reflection.MethodBase[],System.Object[]@,System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[],System.Object@) that caused System.Reflection.Binder.ReorderArgumentArray(System.Object[]@,System.Object) to be called.
[Note: When a method call is bound to a method through reflection using System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,System.Reflection.MethodBase[],System.Object[]@,System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[],System.Object@) , the order, value, and type of the parameters in the original method call can be changed to match the signature of the bound method. The binder creates state as an opaque object that records the original order of the argument array. If, on return from System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,System.Reflection.MethodBase[],System.Object[]@,System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[],System.Object@), state is notnull
, the system calls System.Reflection.Binder.ReorderArgumentArray(System.Object[]@,System.Object). This allows a caller to map the argument array of a method back to the original form if the order had been altered by System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,System.Reflection.MethodBase[],System.Object[]@,System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[],System.Object@) . This is useful ifByRef
arguments are in the argument array, because the caller can retrieve those arguments in their original order on return from this method. When arguments are passed by name (i.e., using named arguments), the binder reorders the argument array and that is what the caller sees. This method insures that the original order of the arguments is restored.]
[Behaviors: state is required to be a non-null Object that tracks the original ordering of args if args is reordered by a call to System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,System.Reflection.MethodBase[],System.Object[]@,System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[],System.Object@) . This method is required to restore the elements of args to their original order, value, and Type]
[Overrides: Implement this method to insure that the parameters contained in args are returned to their original order, Type and value, after being used by a bound method.]
[Usage: Use this method to insure that the parameters contained in args are returned to their original order, Type and value, after being used by a bound method. ]
System.Reflection.Binder Class, System.Reflection Namespace
public abstract MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers);
Selects a method from the specified set of methods, based on the argument type.
- bindingAttr
- A bitwise combination of BindingFlags values that control the binding process. For requirements, see the Behaviors section.
- match
- An array of MethodBase objects that represent the set of methods that Reflection has determined to be a possible match, typically because they have the correct member name.
- types
- An array of Type objects that represent the values used to locate a matching method.
- modifiers
- The only defined value for this parameter is
null
.
A MethodBase instance that reflects the method that is matched to the specified criteria. It is not required that this instance be contained in match . If a suitable method is not found, returnsnull
.
[Behaviors: For the bindingAttr parameter, the caller is required to specify either System.Reflection.BindingFlags.Public or System.Reflection.BindingFlags.NonPublic, and either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static. If at least one value from each pair is not specified, this method is required to returnnull
.]
System.Reflection.Binder Class, System.Reflection Namespace
public abstract PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers);
Selects a property from the specified set of properties, based on the specified criteria.
- bindingAttr
- A bitwise combination of BindingFlags values that control the binding process. For requirements, see the Behaviors section.
- match
- An array of PropertyInfo objects that represent the set of properties that Reflection has determined to be a possible match, typically because they have the correct member name.
- returnType
- The Type of the property being searched for.
- indexes
- An array of Type objects that represent the index types of the property being searched for. [Note: Use this parameter for index properties such as the indexer for a class. ]
- modifiers
- The only defined value for this parameter is
null
.
A PropertyInfo instance that reflects the property that matches the specified criteria. It is not required that this instance be contained in match . If a suitable property is not found, returnsnull
.
[Behaviors: For the bindingAttr parameter, the caller is required to specify either System.Reflection.BindingFlags.Public or System.Reflection.BindingFlags.NonPublic, and either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static. If at least one value from each pair is not specified, this method is required to returnnull
.]
System.Reflection.Binder Class, System.Reflection Namespace