[Flags] |
BindingFlags are used in the following Type methods and other places such as MethodBase.Invoke:
Type.InvokeMember and Type.GetMethod are especially important.
The binding flags are categorized as follows.
Binding Flag | Purpose |
---|---|
DeclaredOnly | Access Control |
FlattenHierarchy | Access Control |
IgnoreCase | Access Control |
IgnoreReturn | Access Control |
Instance | Access Control |
NonPublic | Access Control |
Public | Access Control |
Static | Access Control |
ExactBinding | Change Type |
OptionalParamBinding | Change Type |
CreateInstance | Operation Type |
GetField | Operation Type |
SetField | Operation Type |
GetProperty | Operation Type |
SetProperty | Operation Type |
InvokeMethod | Operation Type |
PutDispProperty | Operation Type |
PutRefDispProperty | Operation Type |
The following table lists the coercions performed by the default Binder.ChangeType. This table applies especially to the ExactBinding binding flag.
Source Type | Target Type |
---|---|
Any type | Its base type. |
Any type | The interface it implements. |
Char | UInt16 , UInt32 , Int32 , UInt64 , Int64 , Single , Double |
Byte | Char , UInt16 , Int16 , UInt32 , Int32 , UInt64 , Int64 , Single , Double |
SByte | Int16 , Int32 , Int64 , Single , Double |
UInt16 | UInt32 , Int32 , UInt64 , Int64 , Single , Double |
Int16 | Int32 , Int64 , Single , Double |
UInt32 | UInt64 , Int64 , Single , Double |
Int32 | Int64 , Single , Double |
UInt64 | Single , Double |
Int64 | Single , Double |
Single | Double |
Non-reference | By-reference. |
//Call an instance method Console.WriteLine(); Console.WriteLine("Invoking an Instance method"); Console.WriteLine("---------------------------------"); tc.GetType().InvokeMember ("AddUp", BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, tc, new object [] {});
//Call an instance method TestClass tc2 = new TestClass(); Console.WriteLine(); Console.WriteLine("DeclaredOnly members"); Console.WriteLine("---------------------------------"); System.Reflection.MemberInfo[] memInfo = tc2.GetType().GetMembers(BindingFlags.DeclaredOnly); for(int i=0;i<memInfo.Length;i++) { Console.WriteLine(memInfo[i].Name); }
//Call the default member of a type Type t3 = typeof (TestClass2); t3.InvokeMember ("", BindingFlags.InvokeMethod | BindingFlags.Default, null, new TestClass2(), new object [] {});
// Get the method named 'MyMethod'. Console.WriteLine("\nDisplaying a method with (int) and (short) parameters.\n"); // Get the method name 'MyMethod' without exact binding. myMethod = myType.GetMethod("MyMethod", new Type[]{typeof(int), typeof(short)}); Console.WriteLine(myMethod); Console.WriteLine("\nDisplaying a method with (int) and (short) parameters.\n"); // Get the method name 'MyMethod' with exact binding. myMethod = myType.GetMethod("MyMethod", BindingFlags.ExactBinding | BindingFlags.Public | BindingFlags.Instance, null, new Type[]{typeof(int), typeof(short)}, null); // Fails to find method with the exact signature as 'MyMethod(int, short)'. Console.WriteLine(myMethod);
// Get all the static fields in a class and its base classes. myType = typeof(MyClassDerived); Console.WriteLine("\nDisplaying the public static fields in 'MyClassDerived'.\n"); myFields = myType.GetFields(BindingFlags.Public | BindingFlags.Static); for(int i = 0; i < myFields.Length; i++) Console.WriteLine(myFields[i]); Console.WriteLine("\nDisplaying the public static fields in all base classes of and in 'MyClassDerived'.\n"); // Get all the public static fields declared in all base classes of and in 'MyClassDerived'. myFields = myType.GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static); for(int i = 0; i < myFields.Length; i++) Console.WriteLine(myFields[i]);
//Get a field value result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {}); Console.WriteLine ("Name == {0}", result);
//Get an indexed property value int index = 3; result = t.InvokeMember ("Item", BindingFlags.GetProperty, null, c, new object [] {index}); Console.WriteLine ("Item[{0}] == {1}", index, result);
// Get the method 'mymethod' irrespective of the case. myType = typeof(MyClass); Console.WriteLine("\nDisplaying the methods named 'mymethod' (case insensitive)\n"); // Get the method named 'MyMethod'. myMethod = myType.GetMethod("mymethod", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); Console.WriteLine(myMethod);
//Call a static method Console.WriteLine(); Console.WriteLine("Invoking static method - PrintName"); Console.WriteLine("---------------------------------"); methInfo = obj.GetType().GetMethod("PrintName"); methInfo.Invoke(obj,BindingFlags.IgnoreReturn | BindingFlags.InvokeMethod, null,new object[] {"Brad","Smith"},null);
//Call an instance method TestClass tc = new TestClass (); Console.WriteLine(); Console.WriteLine("Invoking an Instance method"); Console.WriteLine("---------------------------------"); tc.GetType().InvokeMember ("AddUp", BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, tc, new object [] {});
//Call an instance method TestClass c = new TestClass (); Console.WriteLine(); Console.WriteLine("Invoking an Instance method"); Console.WriteLine("---------------------------------"); c.GetType().InvokeMember ("AddUp", BindingFlags.InvokeMethod, null, c, new object [] {}); c.GetType().InvokeMember ("AddUp", BindingFlags.InvokeMethod, null, c, new object [] {});
MethodInfo [] miNonPublic = type.GetMethods (BindingFlags.Static | BindingFlags.NonPublic); // methods
//Call a static method Console.WriteLine(); Console.WriteLine("Invoking static method - PrintName"); Console.WriteLine("---------------------------------"); methInfo = obj.GetType().GetMethod("PrintName"); methInfo.Invoke(obj,BindingFlags.OptionalParamBinding | BindingFlags.InvokeMethod, null,new object[] {"Brad","Smith"},null);
// Get public fields. Console.WriteLine("\nDisplaying public fields.\n"); myFields = myType.GetFields(BindingFlags.Public | BindingFlags.Instance); for(int i = 0; i < myFields.Length; i++) Console.WriteLine(myFields[i]);
// Set the value of a public field. Console.WriteLine("\nDisplaying the value of a field after setting it.\n"); result = myType.InvokeMember("myFieldD", BindingFlags.SetField, null, myInstance, new object[]{(int)32}, null); Console.WriteLine(myInstance.myFieldD);
// Set the value of a public property. Console.WriteLine("\nDisplaying the value of a property after setting it.\n"); result = myType.InvokeMember("MyProperty", BindingFlags.SetProperty, null, myInstance, new object[]{(int)32}, null); Console.WriteLine(myInstance.MyProperty);
// Get private fields. Console.WriteLine("\nDisplaying private fields.\n"); myFields = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); for(int i = 0; i < myFields.Length; i++) Console.WriteLine(myFields[i]);
CreateInstance | Specifies that Reflection should create an instance of the specified type. Calls the constructor that matches the given arguments. The supplied member name is ignored. If the type of lookup is not specified, (Instance | Public) will apply. It is not possible to call a type initializer. |
DeclaredOnly | Specifies that only members declared at the level of the supplied type's hierarchy should be considered. Inherited members are not considered. |
Default | Specifies no binding flag. |
ExactBinding | Specifies that types of the supplied arguments must exactly match the types of the corresponding formal parameters. When this flag is specified, Type.DefaultBinder is called. Reflection throws an exception if the caller supplies a non-null Binder object, since that implies that the caller is supplying BindToXXX implementations that will pick the appropriate method. Reflection models the accessibility rules of the common type system. For example, if the caller is in the same assembly, the caller does not need special permissions for internal members. Otherwise, the caller needs ReflectionPermission. This is consistent with lookup of members that are protected, private, and so on. The general principle is that Binder.ChangeType should perform only widening coercions, which never lose data. An example of a widening coercion is coercing a value that is a 32-bit signed integer to a value that is a 64-bit signed integer. This is distinguished from a narrowing coercion, which may lose data. An example of a narrowing coercion is coercing a 64-bit signed integer to a 32-bit signed integer. The default binder ignores this flag, while custom binders can implement the semantics of this flag. |
FlattenHierarchy | Specifies that static members up the hierarchy should be returned. Static members include fields, methods, events, and properties. Nested types are not returned. |
GetField | Specifies that the value of the specified field should be returned. |
GetProperty | Specifies that the value of the specified property should be returned. |
IgnoreCase | |
IgnoreReturn | Used in COM Interop to specify that the return value of the member can be ignored. |
Instance | Specifies that instance members are to be included in the search. |
InvokeMethod | Specifies that a method is to be invoked. This may not be a constructor or a type initializer. |
NonPublic | Specifies that non-public members are to be included in the search. |
OptionalParamBinding | Returns the set of members whose parameter count matches the number of supplied arguments. This binding flag is used for methods with parameters that have default values and methods with variable arguments (varargs). This flag should only be used with Type.InvokeMember. Parameters with default values are used only in calls where trailing arguments are omitted. They must be the last arguments. |
Public | Specifies that public members are to be included in the search. |
PutDispProperty | Specifies that the PROPPUT member on a COM object should be invoked.PROPPUT specifies a property-setting function that uses a value. Use PutDispProperty if a property has both PROPPUT and PROPPUTREF and you need to distinguish which one is called. |
PutRefDispProperty | Specifies that the PROPPUTREF member on a COM object should be invoked.PROPPUTREF specifies a property-setting function that uses a reference instead of a value. Use PutRefDispProperty if a property has both PROPPUT and PROPPUTREF and you need to distinguish which one is called. |
SetField | Specifies that the value of the specified field should be set. |
SetProperty | Specifies that the value of the specified property should be set. For COM properties, specifying this binding flag is equivalent to specifying PutDispProperty and PutRefDispProperty. |
Static | Specifies that static members are to be included in the search. |
SuppressChangeType |
Hierarchy: