[英]How to convert fieldinfo to it's actual class object? using C#
i need to create program that navigate an object structure and print the structure of any “struct” provided as an argument. 我需要创建一个导航对象结构并打印任何作为参数提供的“结构”的结构的程序。
These "structs" are defined as follows: 这些“结构”的定义如下:
the problem is when i'm trying to print data member which is a class or struct. 问题是当我尝试打印属于类或结构的数据成员时。 i'm trying to to write a recursive function that gets an object and check each field in the object: if it's a class them i send again the curr field to the same function.
我试图编写一个获取对象并检查对象中每个字段的递归函数:如果它是一个类,我将curr字段再次发送给同一函数。 else i print the value of the field.
否则我会打印该字段的值。
this is my code. 这是我的代码。 but when i send the fieldInfo to the function the code line: --->
Type objType = i_obj.GetType();
但是当我将fieldInfo发送到函数的代码行时:--->
Type objType = i_obj.GetType();
is getting me the next value: object.GetType
returned {Name = "RtFieldInfo" FullName = System.Reflection.RtFieldInfo"} System.RuntimeType
正在获取下一个值:
object.GetType
返回{Name = "RtFieldInfo" FullName = System.Reflection.RtFieldInfo"} System.RuntimeType
public static void getObj(object i_obj)
{
Type objType = i_obj.GetType();
FieldInfo[] objField = objType.GetFields();
foreach (FieldInfo member in objField)
{
Type memberType = member.FieldType;
if(memberType.IsClass)
{
getObj(member);
}
else
{
Console.WriteLine(member.Name + " : " + member.GetValue(i_obj));
}
}
}
how can i get the real object from fieldInfo ?? 我如何从fieldInfo获取真实对象?
For the fields where IsClass
is true you want to pass on the value of the field to the nested call to getObj
. 对于
IsClass
为true的字段,您想将该字段的值传递给getObj
的嵌套调用。 Also you might want to do some null
checks: 另外,您可能需要做一些
null
检查:
public static void getObj(object i_obj)
{
Type objType = i_obj.GetType();
FieldInfo[] objField = objType.GetFields();
foreach (FieldInfo member in objField)
{
Type memberType = member.FieldType;
object memberValue = member.GetValue(i_obj); // <---
if (memberValue == null)
{
Console.WriteLine(member.Name + " : null");
}
else if(memberType.IsClass)
{
getObj(memberValue); // <---
}
else
{
Console.WriteLine(member.Name + " : " + memberValue);
}
}
}
When you do this: 执行此操作时:
if(memberType.IsClass)
{
getObj(member);
}
you're calling the method recursively, but what you're passing back into it is member
, which is a FieldInfo
. 您正在递归地调用该方法,但是传递回该方法的是
member
,它是FieldInfo
。 So instead of looking recursively at the fields of the field's value, you're going to look at the properties of the System.Reflection.FieldInfo
which I'm sure isn't what you want. 因此,您将不必查看递归地查看字段值的字段,而是查看
System.Reflection.FieldInfo
的属性,我确定这不是您想要的。
Perhaps what you want is this: 也许您想要的是这样的:
if(memberType.IsClass)
{
getObj(member.GetValue(i_obj));
}
Now you're getting the value from the field - an object of some sort - and recursively calling your method with that. 现在,您从字段(某种对象)中获取值,并以此递归地调用您的方法。
I don't know what this is for, but chances are that at some point you'll need to account for collections. 我不知道这是干什么的,但是有可能在某些时候您需要考虑馆藏。 Presumably if the field is an array of strings you'll want to return the individual strings.
如果该字段是一个字符串数组,则可能需要返回单个字符串。 Or if it's a list of some object, you'll want to iterate over the items in the list, not the members of
List<T>
. 或者,如果它是某个对象的列表,则将要遍历列表中的项目,而不是
List<T>
的成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.