·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> GetProperties(BindingFlags)说明
Instance|Public:获取公共的的实例属性(非静态的)
Instance|NonPublic:获取非公共的的实例属性(非静态的)。(private/protect/internal)
Static|Public:获取公共的静态属性
Static|NonPublic:获取非公共的静态属性。(private/protect/internal)
Instance|Static|Public:获取公共的的实例或静态属性
Instance|Static|NonPublic:非获取公共的的实例或静态属性
DemoCode
类型定义:
class TestClass : TestClassBase{ private string pub; public string Pub { get { return pub; } set { pub = value; } } private string pubGet; public string PubGet { get { return pubGet; } } private string pubprotected; protected string Pubprotected { get { return pubprotected; } set { pubprotected = value; } } private string pubprotectedGet; protected string PubprotectedGet { get { return pubprotectedGet; } } private string pubinternal; internal string Pubinternal { get { return pubinternal; } set { pubinternal = value; } } private string pubGet_internal; internal string PubGet_internal { get { return pubGet_internal; } } private string pub_private; private string Pub_private { get { return pub_private; } set { pub_private = value; } } private string pubGet_private; private string PubGet_private { get { return pubGet_private; } }}class TestClassBase{ private static string pub_static; public static string Pub_static { get { return pub_static; } set { pub_static = value; } } private static string pubGet_static; public static string PubGet_static { get { return pubGet_static; } } private static string pubprotected_static; protected static string Pubprotected_static { get { return pubprotected_static; } set { pubprotected_static = value; } } private static string pubprotectedGet_static; protected static string PubprotectedGet_static { get { return pubprotectedGet_static; } } private static string pubinternal_static; internal static string Pubinternal_static { get { return pubinternal_static; } set { pubinternal_static = value; } } private static string pubGet_internal_static; internal static string PubGet_internal_static { get { return pubGet_internal_static; } } private static string pub_private_static; private static string Pub_private_static { get { return pub_private_static; } set { pub_private_static = value; } } private static string pubGet_private_static; private static string PubGet_private_static { get { return pubGet_private_static; } }}
功能:
class Program { static void Main(string[] args) { PropertyInfo[] pis; //输出默认的方法获取的属性 Console.WriteLine("None"); pis = typeof(TestClass).GetProperties(); Console.WriteLine(typeof(TestClass).Name); pis.ToList().ForEach(s => Console.WriteLine("\t: {0}", s)); Console.WriteLine(typeof(TestClassBase).Name); pis = typeof(TestClassBase).GetProperties(); pis.ToList().ForEach(s => Console.WriteLine("\t: {0}", s)); Console.WriteLine(); //获取到全组和 var all = AllBindFlags; //已经操作过的 List<BindingFlags> lst = new List<BindingFlags>(); //遍历全组合 foreach (var child in all) { //组合转换为 BindingFlags BindingFlags v = child[0]; for (int i = 1; i < child.Count; i++) { v |= child[i]; } if (lst.Contains(v)) continue; lst.Add(v); Console.WriteLine(v); try { pis = typeof(TestClass).GetProperties(v); Console.WriteLine(typeof(TestClass).Name); pis.ToList().ForEach(s => Console.WriteLine("\t: {0}", s)); Console.WriteLine(typeof(TestClassBase).Name); pis = typeof(TestClassBase).GetProperties(v); pis.ToList().ForEach(s => Console.WriteLine("\t: {0}", s)); Console.WriteLine("*".PadRight(10, '*')); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.Read(); } Console.Read(); } private static BindingFlags[] bindFlags; public static BindingFlags[] BindFlags { get { if (Program.bindFlags == null) { List<BindingFlags> lst = new List<BindingFlags>(); foreach (var str in Enum.GetNames(typeof(BindingFlags))) { lst.Add((BindingFlags)Enum.Parse(typeof(BindingFlags), str)); } Program.bindFlags = lst.ToArray(); } return Program.bindFlags; } } private static List<List<BindingFlags>> allbindFlags; /// <summary> /// 全组合 /// </summary> public static List<List<BindingFlags>> AllBindFlags { get { if (Program.allbindFlags == null) { var val = FullCombination<BindingFlags>(new List<BindingFlags>(BindFlags)); val = val.Distinct<List<BindingFlags>>(new BindingFlagEqual()).ToList(); val.Sort(new BindingFlagCompare()); allbindFlags = val; } return Program.allbindFlags; } } public static List<T> GetEnumList<T>() { var lst = new List<T>(); foreach (T item in Enum.GetValues(typeof(T))) { lst.Add(item); } return lst; } //全组合算法 public static List<List<T>> FullCombination<T>(List<T> lstSource) { var n = lstSource.Count; var max = 1 << n; var lstResult = new List<List<T>>(); for (var i = 0; i < max; i++) { var lstTemp = new List<T>(); for (var j = 0; j < n; j++) { if ((i >> j & 1) > 0) { lstTemp.Add(lstSource[j]); } } lstResult.Add(lstTemp); } lstResult.RemoveAt(0); return lstResult; } } } class BindingFlagEqual : IEqualityComparer<List<BindingFlags>> { #region IEqualityComparer<List<BindingFlags>> 成员 public bool Equals(List<BindingFlags> x, List<BindingFlags> y) { BindingFlags bind1 = ToBind(x); BindingFlags bind2 = ToBind(y); return bind1 == bind2; } private BindingFlags ToBind(List<BindingFlags> x) { BindingFlags bind = x[0]; for (int i = 1; i < x.Count; i++) { bind = bind | x[i]; } return bind; } public int GetHashCode(List<BindingFlags> obj) { return ToBind(obj).GetHashCode(); } #endregion } class BindingFlagCompare : Comparer<List<BindingFlags>> { public override int Compare(List<BindingFlags> x, List<BindingFlags> y) { var v = x.Count.CompareTo(y.Count); if (v == 0) { BindingFlags bind1 = ToBind(x); BindingFlags bind2 = ToBind(y); return bind1.CompareTo(bind2); } return v; } private BindingFlags ToBind(List<BindingFlags> x) { BindingFlags bind