.Net Framework 4.0 以下,包括4.0
Type pPt = typeof(People); System.Reflection.PropertyInfo[] tPro = pPt.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (var d in tPro) { //属性只读判断 if (d.GetGetMethod() != null && d.GetSetMethod()==null) { } //属性只写判断 if (d.GetGetMethod() == null && d.GetSetMethod() != null) { } } Console.Read();
.Net Framework 4.5 以上,包括4.5
Type pPt = typeof(People); System.Reflection.PropertyInfo[] tPro = pPt.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (var d in tPro) { //属性只读判断 if (d.GetMethod != null && d.SetMethod==null) { Console.WriteLine(d.Name + ":只读"); } //属性只写判断 if (d.GetMethod == null && d.SetMethod != null) { Console.WriteLine(d.Name + ":只写"); } }
作者:
cglnet
本文版权归cglNet和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.