我有一个类Class1
获取领域
public class Class1
{
public string ABC { get; set; }
public string DEF { get; set; }
public string GHI { get; set; }
public string JLK { get; set; }
}
我怎样才能得到的名单,在这种情况下,“ABC”,“DEF”,... 我想获取所有公共领域的名称。
我试过如下:
Dictionary<string, string> props = new Dictionary<string, string>();
foreach (var prop in classType.GetType().GetProperties().Where(x => x.CanWrite == true).ToList())
{
//Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(classitem, null));
//objectItem.SetValue("ABC", "Test");
props.Add(prop.Name, "");
}
和:
var bindingFlags = BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public;
var fieldValues = classType.GetType()
.GetFields(bindingFlags)
.Select(field => field.GetValue(classType))
.ToList();
但无论是给了我想要的结果。
在此先感谢
只有''GetProperties()''应该可以工作 – 2015-04-01 16:43:27
什么,具体来说,是不是你的尝试解决方案的工作? – Servy 2015-04-01 16:44:17
看一看:[字段和C#中的属性有什么区别?](http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a -property-in-c) – 2015-04-01 16:46:55