Description
This enumeration is used by reflection classes such as Binder, Module, and ConstructorInfo. BindingFlags values are used to control binding in methods in classes that find and invoke, create, get, and set members and types.
To specify multiple BindingFlags values, use the bitwise "OR" operator.
Example
using System;
using System.Reflection;
/// <summary>
/// This example shows the varoius ways of using BindingFlags.
/// First we should al the members of object, then we invoke a
/// method off of System.String. Then we invoke a property off
/// string.
/// </summary>
public class BindingFlagsSample
{
public static void Main()
{
Console.WriteLine("Public Members: DeclaredOnly");
foreach (MemberInfo m in typeof(Int32).GetMembers(
BindingFlags.Instance | BindingFlags.Static |
BindingFlags.Public | BindingFlags.DeclaredOnly))
{
Console.WriteLine(" {0}",m.Name);
}
Console.WriteLine("Public Members: FlattenHierarchy");
foreach (MemberInfo m in typeof(Int32).GetMembers(
BindingFlags.Instance | BindingFlags.Static |
BindingFlags.Public | BindingFlags.FlattenHierarchy))
{
Console.WriteLine(" {0}", m.Name);
}
Console.WriteLine("Protected and Private Members: FlattenHierarchy");
foreach (MemberInfo m in typeof(Int32).GetMembers(
BindingFlags.Instance | BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.FlattenHierarchy))
{
Console.WriteLine(" {0}", m.Name);
}
string s = "Life, The Universe, and Everything ";
Type type = typeof(String);
int length;
//Latebound equivalent to: s = s.Remove(0,6);
s = (string) type.InvokeMember ("Remove",BindingFlags.InvokeMethod,
null, s, new Object [] {0,6});
//Latebound equivalent to: length = s.Length;
length = (int)type.InvokeMember("Length", BindingFlags.GetProperty,
null, s, null);
Console.WriteLine("'{0}'", s);
Console.WriteLine("Length = {0}", length);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
}
}
The output is
Public Members: DeclaredOnly
MaxValue
MinValue
ToString
GetTypeCode
ToString
CompareTo
GetHashCode
Equals
ToString
ToString
Parse
Parse
Parse
Parse
Public Members: FlattenHierarchy
MaxValue
MinValue
ToString
GetTypeCode
ToString
CompareTo
GetHashCode
Equals
ToString
ToString
Parse
Parse
Parse
Parse
GetType
Equals
ReferenceEquals
Protected and Private Members: FlattenHierarchy
m_value
System.IConvertible.ToType
System.IConvertible.ToDateTime
System.IConvertible.ToDecimal
System.IConvertible.ToDouble
System.IConvertible.ToSingle
System.IConvertible.ToUInt64
System.IConvertible.ToInt64
System.IConvertible.ToUInt32
System.IConvertible.ToInt32
System.IConvertible.ToUInt16
System.IConvertible.ToInt16
System.IConvertible.ToByte
System.IConvertible.ToSByte
System.IConvertible.ToChar
System.IConvertible.ToBoolean
Finalize
MemberwiseClone
'The Universe, and Everything '
Length = 29
Press Enter to continue
|