如何在 C# 中找到对象的所有公共字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/237275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find all the public fields of an object in C#?
提问by junkforce
I'm constructing a method to take in an ArrayList(presumably full of objects) and then list all the fields(and their values) for each object in the ArrayList.
我正在构建一个方法来接收一个 ArrayList(大概充满对象),然后列出 ArrayList 中每个对象的所有字段(及其值)。
Currently my code is as follows:
目前我的代码如下:
public static void ListArrayListMembers(ArrayList list)
{
foreach (Object obj in list)
{
Type type = obj.GetType();
string field = type.GetFields().ToString();
Console.WriteLine(field);
}
}
Of course, I understand the immediate issue with this code: if it worked it'd only print one field per object in the ArrayList. I'll fix this later - right now I'm just curious how to get all of the public fields associated with an object.
当然,我理解这段代码的直接问题:如果它有效,它只会在 ArrayList 中为每个对象打印一个字段。我稍后会解决这个问题 - 现在我只是好奇如何获取与对象关联的所有公共字段。
采纳答案by Dave Markle
foreach (Object obj in list) {
Type type = obj.GetType();
foreach (var f in type.GetFields().Where(f => f.IsPublic)) {
Console.WriteLine(
String.Format("Name: {0} Value: {1}", f.Name, f.GetValue(obj));
}
}
Note that this code requires .NET 3.5 to work ;-)
请注意,此代码需要 .NET 3.5 才能工作;-)
回答by nyxtom
public static void ListArrayListMembers(ArrayList list)
{
foreach (Object obj in list)
{
Type type = obj.GetType();
Console.WriteLine("{0} -- ", type.Name);
Console.WriteLine(" Properties: ");
foreach (PropertyInfo prop in type.GetProperties())
{
Console.WriteLine("\t{0} {1} = {2}", prop.PropertyType.Name,
prop.Name, prop.GetValue(obj, null));
}
Console.WriteLine(" Fields: ");
foreach (FieldInfo field in type.GetFields())
{
Console.WriteLine("\t{0} {1} = {2}", field.FieldType.Name,
field.Name, field.GetValue(obj));
}
}
}
I'd like to mention that looking for IsPublic in the fields is not necessary as type.GetFields() as defined by MSDNstates:
我想提一下,在字段中查找 IsPublic 不是必需的,因为MSDN状态定义的 type.GetFields() :
Return Value - Type: System.Reflection.FieldInfo[]
返回值 - 类型:System.Reflection.FieldInfo[]
An array of FieldInfo objects representing all the public fieldsdefined for the current Type.
表示为当前类型定义的所有公共字段的 FieldInfo 对象数组。
回答by Jon B
static void ListArrayListMembers(ArrayList list)
{
foreach (object obj in list)
{
Type type = obj.GetType();
foreach (FieldInfo field in type.GetFields(BindingFlags.Public))
{
Console.WriteLine(field.Name + " = " + field.GetValue(obj).ToString());
}
}
}
回答by Jonathan Webb
You can obtain all the object Fields declared directly in the class with the BindingFlags:
您可以使用 BindingFlags 获取直接在类中声明的所有对象字段:
GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
and all object Fields including inherited with:
以及所有对象字段,包括继承自:
GetFields(BindingFlags.Public | BindingFlags.Instance)
回答by Marc Gravell
Of course, another question would be "why have you got public fields?" - properties being preferable. As an abstraction, note that reflection isn't the only option: it is also possible for a type to expose it's properties on-the-fly at runtime (like how an untyped DataTable
/DataView
exposes columns as properties).
当然,另一个问题是“你为什么有公共领域?” - 属性更可取。作为一种抽象,请注意反射不是唯一的选择:类型也可以在运行时即时公开其属性(例如无类型DataTable
/DataView
将列作为属性公开的方式)。
To support this (while also supporting simple objects), you would use TypeDescriptor
:
为了支持这一点(同时也支持简单的对象),你可以使用TypeDescriptor
:
foreach(PropertyDescriptor prop in TypeDescriptor.GetProperties(obj))
{
Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(obj));
}
This also allows for numerous extensibility options - for example, vastly speeding up reflection(without changing any code).
这也允许许多可扩展性选项 - 例如,极大地加速反射(无需更改任何代码)。