BindingFlags 枚举的用法
写这个想法:就是
如果想比较一个enum类中的枚举项是否相同的话。
可以用下面的这个语句:
BindingFlags selectedBindingFlags = (BindingFlags)Enum.Parse(typeof(BindingFlags), this.comboBox1.SelectedValue.ToString(), true);
* public enum BindingFlags是枚举类
ArrayList list= new ArrayList()以下都是向 ComboBox控件中添加Item,Value。
从comboBox中选择一项之后,如:选择Public项,然后通过上面这个语句,从BindingFlags类的去寻找相同的EnumItem,如果有的话,Return true,没有也不会error
如果想比较一个enum类中的枚举项是否相同的话。
可以用下面的这个语句:
BindingFlags selectedBindingFlags = (BindingFlags)Enum.Parse(typeof(BindingFlags), this.comboBox1.SelectedValue.ToString(), true);
* public enum BindingFlags是枚举类
ArrayList list= new ArrayList()以下都是向 ComboBox控件中添加Item,Value。
从comboBox中选择一项之后,如:选择Public项,然后通过上面这个语句,从BindingFlags类的去寻找相同的EnumItem,如果有的话,Return true,没有也不会error
public enum BindingFlags
{ Default = 0, IgnoreCase = 1, DeclaredOnly = 2, Instance = 4, Static = 8, Public = 16, NonPublic = 32, FlattenHierarchy = 64, InvokeMethod = 256, CreateInstance = 512, GetField = 1024, SetField = 2048, GetProperty = 4096, SetProperty = 8192, PutDispProperty = 16384, PutRefDispProperty = 32768, ExactBinding = 65536, SuppressChangeType = 131072, OptionalParamBinding = 262144, IgnoreReturn = 16777216, } } |
ArrayList list = new ArrayList();
list.Add(new DictionaryEntry("0", "Default")); list.Add(new DictionaryEntry("2", "IgnoreCase")); list.Add(new DictionaryEntry("3", "DeclaredOnly")); list.Add(new DictionaryEntry("4", "Instance")); list.Add(new DictionaryEntry("8", "Static")); list.Add(new DictionaryEntry("16", "Public")); list.Add(new DictionaryEntry("32", "NonPublic")); list.Add(new DictionaryEntry("64", "FlattenHierarchy")); list.Add(new DictionaryEntry("256", "InvokeMethod")); list.Add(new DictionaryEntry("512", "CreateInstance")); list.Add(new DictionaryEntry("1024", "GetField")); list.Add(new DictionaryEntry("2048", "SetField")); list.Add(new DictionaryEntry("4096", "GetProperty")); list.Add(new DictionaryEntry("8192", "SetProperty")); list.Add(new DictionaryEntry("16384", "PutDispProperty")); list.Add(new DictionaryEntry("32768", "PutRefDispProperty")); list.Add(new DictionaryEntry("65536", "ExactBinding")); list.Add(new DictionaryEntry("131072", "SuppressChangeType")); list.Add(new DictionaryEntry("262144O", "ptionalParamBinding")); list.Add(new DictionaryEntry("16777216", "IgnoreReturn")); comboBox1.DataSource = list; comboBox1.DisplayMember = "Value"; comboBox1.ValueMember = "Key"; |
最后贴一个BindingFlags的Demo Share with EveryOne
-----------------------------------------------
using System;
using System.Reflection;
using System.IO;
namespace BindingFlagsSnippet
{
class EntryPoint
{
static void Main(string[] args)
{
Invoke.Go();
}
}
class Invoke
{
public static void Go()
{
// BindingFlags.InvokeMethod
// Call a static method.
Type t = typeof (TestClass);
Console.WriteLine();
Console.WriteLine("Invoking a static method.");
Console.WriteLine("-------------------------");
t.InvokeMember ("SayHello", BindingFlags.InvokeMethod, null, null, new object [] {});
// BindingFlags.InvokeMethod
// 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 [] {});
// BindingFlags.InvokeMethod
// Call a method with parameters.
object [] args = new object [] {100.09, 184.45};
object result;
Console.WriteLine();
Console.WriteLine("Invoking a method with parameters.");
Console.WriteLine("---------------------------------");
result = t.InvokeMember ("ComputeSum", BindingFlags.InvokeMethod, null, null, args);
Console.WriteLine ("{0} + {1} = {2}", args[0], args[1], result);
// BindingFlags.GetField, SetField
Console.WriteLine();
Console.WriteLine("Invoking a field (getting and setting.)");
Console.WriteLine("--------------------------------------");
// Get a field value.
result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {});
Console.WriteLine ("Name == {0}", result);
// Set a field.
t.InvokeMember ("Name", BindingFlags.SetField, null, c, new object [] {"NewName"});
result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {});
Console.WriteLine ("Name == {0}", result);
Console.WriteLine();
Console.WriteLine("Invoking an indexed property (getting and setting.)");
Console.WriteLine("--------------------------------------------------");
// BindingFlags.GetProperty
// 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);
// BindingFlags.SetProperty
// Set an indexed property value.
index = 3;
t.InvokeMember ("Item", BindingFlags.SetProperty, null, c, new object [] {index, "NewValue"});
result = t.InvokeMember ("Item", BindingFlags.GetProperty , null, c, new object [] {index});
Console.WriteLine ("Item[{0}] == {1}", index, result);
Console.WriteLine();
Console.WriteLine("Getting a field or property.");
Console.WriteLine("----------------------------");
// BindingFlags.GetField
// Get a field or property.
result = t.InvokeMember ("Name", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
Console.WriteLine ("Name == {0}", result);
// BindingFlags.GetProperty
result = t.InvokeMember ("Value", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
Console.WriteLine ("Value == {0}", result);
Console.WriteLine();
Console.WriteLine("Invoking a method with named parameters.");
Console.WriteLine("---------------------------------------");
// BindingFlags.InvokeMethod
// Call a method using named parameters.
object[] argValues = new object [] {"Mouse", "Micky"};
String [] argNames = new String [] {"lastName", "firstName"};
t.InvokeMember ("PrintName", BindingFlags.InvokeMethod, null, null, argValues, null, null, argNames);
Console.WriteLine();
Console.WriteLine("Invoking a default member of a type.");
Console.WriteLine("------------------------------------");
// BindingFlags.Default
// Call the default member of a type.
Type t3 = typeof (TestClass2);
t3.InvokeMember ("", BindingFlags.InvokeMethod | BindingFlags.Default, null, new TestClass2(), new object [] {});
// BindingFlags.Static, NonPublic, and Public
// Invoking a member by reference.
Console.WriteLine();
Console.WriteLine("Invoking a method by reference.");
Console.WriteLine("-------------------------------");
MethodInfo m = t.GetMethod("Swap");
args = new object[2];
args[0] = 1;
args[1] = 2;
m.Invoke(new TestClass(),args);
Console.WriteLine ("{0}, {1}", args[0], args[1]);
// The string is case-sensitive.
Type type = Type.GetType("System.String");
// Check to see if the value is valid. If the object is null, the type does not exist.
if (type == null)
{
Console.WriteLine("Please ensure that you specify only valid types in the type field.");
Console.WriteLine("The type name is case-sensitive.");
return;
}
// Declare and populate the arrays to hold the information.
// You must declare either NonPublic or Public with Static or the search will not work.
FieldInfo [] fi = type.GetFields (BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public);
// BindingFlags.NonPublic
MethodInfo [] miNonPublic = type.GetMethods (BindingFlags.Static |
BindingFlags.NonPublic);
// BindingFlags.Public
MethodInfo [] miPublic = type.GetMethods (BindingFlags.Static |
BindingFlags.Public);
// Iterate through all the nonpublic methods.
foreach (MethodInfo method in miNonPublic)
{
Console.WriteLine(method);
}
// Iterate through all the public methods.
foreach (MethodInfo method in miPublic)
{
Console.WriteLine(method);
}
// Iterate through all the fields.
foreach (FieldInfo f in fi)
{
Console.WriteLine(f);
}
// BindingFlags.Instance
// 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 [] {});
// BindingFlags.CreateInstance
// Calling and creating an instance method.
Console.WriteLine();
Console.WriteLine("Invoking and creating an instance method.");
Console.WriteLine("-----------------------------------------");
tc.GetType().InvokeMember ("AddUp", BindingFlags.Public |
BindingFlags.Instance | BindingFlags.CreateInstance,
null, tc, new object [] {});
// BindingFlags.DeclaredOnly
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);
}
// BindingFlags.SuppressChangeType
TestClass obj = new TestClass();
Console.WriteLine();
Console.WriteLine("Invoking static method - PrintName");
Console.WriteLine("---------------------------------");
System.Reflection.MethodInfo methInfo =
obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.SuppressChangeType |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null);
// BindingFlags.IgnoreCase
Console.WriteLine();
Console.WriteLine("Using IgnoreCase and invoking the PrintName method.");
Console.WriteLine("---------------------------------------------------");
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.IgnoreCase |
BindingFlags.InvokeMethod, null,new object[]
{"brad","smith"},null);
// BindingFlags.IgnoreReturn
Console.WriteLine();
Console.WriteLine("Using IgnoreReturn and invoking the PrintName method.");
Console.WriteLine("-----------------------------------------------------");
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.IgnoreReturn |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null);
// BindingFlags.OptionalParamBinding
Console.WriteLine();
Console.WriteLine("Using OptionalParamBinding and invoking the PrintName method.");
Console.WriteLine("-------------------------------------------------------------");
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.OptionalParamBinding |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null);
// BindingFlags.ExactBinding
Console.WriteLine();
Console.WriteLine("Using ExactBinding and invoking the PrintName method.");
Console.WriteLine("-----------------------------------------------------");
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.ExactBinding |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null);
// BindingFlags.FlattenHierarchy
Console.WriteLine();
Console.WriteLine("Using FlattenHierarchy and invoking the PrintName method.");
Console.WriteLine("---------------------------------------------------------");
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.FlattenHierarchy |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null);
}
}
public class TestClass
{
public String Name;
private Object [] values = new Object [] {0, 1,2,3,4,5,6,7,8,9};
public Object this [int index]
{
get
{
return values[index];
}
set
{
values[index] = value;
}
}
public Object Value
{
get
{
return "the value";
}
}
public TestClass ()
{
Name = "initialName";
}
int methodCalled = 0;
public static void SayHello ()
{
Console.WriteLine ("Hello");
}
public void AddUp ()
{
methodCalled++;
Console.WriteLine ("AddUp Called {0} times", methodCalled);
}
public static double ComputeSum (double d1, double d2)
{
return d1 + d2;
}
public static void PrintName (String firstName, String lastName)
{
Console.WriteLine ("{0},{1}", lastName,firstName);
}
public void PrintTime ()
{
Console.WriteLine (DateTime.Now);
}
public void Swap(ref int a, ref int b)
{
int x = a;
a = b;
b = x;
}
}
[DefaultMemberAttribute ("PrintTime")]
public class TestClass2
{
public void PrintTime ()
{
Console.WriteLine (DateTime.Now);
}
}
}
-----------------------------------------------
using System;
using System.Reflection;
using System.IO;
namespace BindingFlagsSnippet
{
class EntryPoint
{
static void Main(string[] args)
{
Invoke.Go();
}
}
class Invoke
{
public static void Go()
{
// BindingFlags.InvokeMethod
// Call a static method.
Type t = typeof (TestClass);
Console.WriteLine();
Console.WriteLine("Invoking a static method.");
Console.WriteLine("-------------------------");
t.InvokeMember ("SayHello", BindingFlags.InvokeMethod, null, null, new object [] {});
// BindingFlags.InvokeMethod
// 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 [] {});
// BindingFlags.InvokeMethod
// Call a method with parameters.
object [] args = new object [] {100.09, 184.45};
object result;
Console.WriteLine();
Console.WriteLine("Invoking a method with parameters.");
Console.WriteLine("---------------------------------");
result = t.InvokeMember ("ComputeSum", BindingFlags.InvokeMethod, null, null, args);
Console.WriteLine ("{0} + {1} = {2}", args[0], args[1], result);
// BindingFlags.GetField, SetField
Console.WriteLine();
Console.WriteLine("Invoking a field (getting and setting.)");
Console.WriteLine("--------------------------------------");
// Get a field value.
result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {});
Console.WriteLine ("Name == {0}", result);
// Set a field.
t.InvokeMember ("Name", BindingFlags.SetField, null, c, new object [] {"NewName"});
result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {});
Console.WriteLine ("Name == {0}", result);
Console.WriteLine();
Console.WriteLine("Invoking an indexed property (getting and setting.)");
Console.WriteLine("--------------------------------------------------");
// BindingFlags.GetProperty
// 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);
// BindingFlags.SetProperty
// Set an indexed property value.
index = 3;
t.InvokeMember ("Item", BindingFlags.SetProperty, null, c, new object [] {index, "NewValue"});
result = t.InvokeMember ("Item", BindingFlags.GetProperty , null, c, new object [] {index});
Console.WriteLine ("Item[{0}] == {1}", index, result);
Console.WriteLine();
Console.WriteLine("Getting a field or property.");
Console.WriteLine("----------------------------");
// BindingFlags.GetField
// Get a field or property.
result = t.InvokeMember ("Name", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
Console.WriteLine ("Name == {0}", result);
// BindingFlags.GetProperty
result = t.InvokeMember ("Value", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
Console.WriteLine ("Value == {0}", result);
Console.WriteLine();
Console.WriteLine("Invoking a method with named parameters.");
Console.WriteLine("---------------------------------------");
// BindingFlags.InvokeMethod
// Call a method using named parameters.
object[] argValues = new object [] {"Mouse", "Micky"};
String [] argNames = new String [] {"lastName", "firstName"};
t.InvokeMember ("PrintName", BindingFlags.InvokeMethod, null, null, argValues, null, null, argNames);
Console.WriteLine();
Console.WriteLine("Invoking a default member of a type.");
Console.WriteLine("------------------------------------");
// BindingFlags.Default
// Call the default member of a type.
Type t3 = typeof (TestClass2);
t3.InvokeMember ("", BindingFlags.InvokeMethod | BindingFlags.Default, null, new TestClass2(), new object [] {});
// BindingFlags.Static, NonPublic, and Public
// Invoking a member by reference.
Console.WriteLine();
Console.WriteLine("Invoking a method by reference.");
Console.WriteLine("-------------------------------");
MethodInfo m = t.GetMethod("Swap");
args = new object[2];
args[0] = 1;
args[1] = 2;
m.Invoke(new TestClass(),args);
Console.WriteLine ("{0}, {1}", args[0], args[1]);
// The string is case-sensitive.
Type type = Type.GetType("System.String");
// Check to see if the value is valid. If the object is null, the type does not exist.
if (type == null)
{
Console.WriteLine("Please ensure that you specify only valid types in the type field.");
Console.WriteLine("The type name is case-sensitive.");
return;
}
// Declare and populate the arrays to hold the information.
// You must declare either NonPublic or Public with Static or the search will not work.
FieldInfo [] fi = type.GetFields (BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public);
// BindingFlags.NonPublic
MethodInfo [] miNonPublic = type.GetMethods (BindingFlags.Static |
BindingFlags.NonPublic);
// BindingFlags.Public
MethodInfo [] miPublic = type.GetMethods (BindingFlags.Static |
BindingFlags.Public);
// Iterate through all the nonpublic methods.
foreach (MethodInfo method in miNonPublic)
{
Console.WriteLine(method);
}
// Iterate through all the public methods.
foreach (MethodInfo method in miPublic)
{
Console.WriteLine(method);
}
// Iterate through all the fields.
foreach (FieldInfo f in fi)
{
Console.WriteLine(f);
}
// BindingFlags.Instance
// 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 [] {});
// BindingFlags.CreateInstance
// Calling and creating an instance method.
Console.WriteLine();
Console.WriteLine("Invoking and creating an instance method.");
Console.WriteLine("-----------------------------------------");
tc.GetType().InvokeMember ("AddUp", BindingFlags.Public |
BindingFlags.Instance | BindingFlags.CreateInstance,
null, tc, new object [] {});
// BindingFlags.DeclaredOnly
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);
}
// BindingFlags.SuppressChangeType
TestClass obj = new TestClass();
Console.WriteLine();
Console.WriteLine("Invoking static method - PrintName");
Console.WriteLine("---------------------------------");
System.Reflection.MethodInfo methInfo =
obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.SuppressChangeType |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null);
// BindingFlags.IgnoreCase
Console.WriteLine();
Console.WriteLine("Using IgnoreCase and invoking the PrintName method.");
Console.WriteLine("---------------------------------------------------");
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.IgnoreCase |
BindingFlags.InvokeMethod, null,new object[]
{"brad","smith"},null);
// BindingFlags.IgnoreReturn
Console.WriteLine();
Console.WriteLine("Using IgnoreReturn and invoking the PrintName method.");
Console.WriteLine("-----------------------------------------------------");
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.IgnoreReturn |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null);
// BindingFlags.OptionalParamBinding
Console.WriteLine();
Console.WriteLine("Using OptionalParamBinding and invoking the PrintName method.");
Console.WriteLine("-------------------------------------------------------------");
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.OptionalParamBinding |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null);
// BindingFlags.ExactBinding
Console.WriteLine();
Console.WriteLine("Using ExactBinding and invoking the PrintName method.");
Console.WriteLine("-----------------------------------------------------");
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.ExactBinding |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null);
// BindingFlags.FlattenHierarchy
Console.WriteLine();
Console.WriteLine("Using FlattenHierarchy and invoking the PrintName method.");
Console.WriteLine("---------------------------------------------------------");
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.FlattenHierarchy |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null);
}
}
public class TestClass
{
public String Name;
private Object [] values = new Object [] {0, 1,2,3,4,5,6,7,8,9};
public Object this [int index]
{
get
{
return values[index];
}
set
{
values[index] = value;
}
}
public Object Value
{
get
{
return "the value";
}
}
public TestClass ()
{
Name = "initialName";
}
int methodCalled = 0;
public static void SayHello ()
{
Console.WriteLine ("Hello");
}
public void AddUp ()
{
methodCalled++;
Console.WriteLine ("AddUp Called {0} times", methodCalled);
}
public static double ComputeSum (double d1, double d2)
{
return d1 + d2;
}
public static void PrintName (String firstName, String lastName)
{
Console.WriteLine ("{0},{1}", lastName,firstName);
}
public void PrintTime ()
{
Console.WriteLine (DateTime.Now);
}
public void Swap(ref int a, ref int b)
{
int x = a;
a = b;
b = x;
}
}
[DefaultMemberAttribute ("PrintTime")]
public class TestClass2
{
public void PrintTime ()
{
Console.WriteLine (DateTime.Now);
}
}
}