简体   繁体   English

如何将fieldinfo转换为其实际的类对象? 使用C#

[英]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: 这些“结构”的定义如下:

  • They have only public attributes 它们只有公共属性
  • Each attribute can be of the following types: 每个属性可以具有以下类型:
    • “Structs” “结构”
    • Primitive (eg int), primitive wrapper (eg Integer) or String 原始(例如int),原始包装器(例如Integer)或字符串

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.

相关问题 转换 CookComputing XMLRpcStruct (IEnumerable<Object> ) 到实际的 C# 类 - Convert CookComputing XMLRpcStruct (IEnumerable<Object>) to an actual C# class 如何获得 System.Collections.Generic.List<fieldinfo> 它包含 class 层次结构中类型 T 的 object 到 Object 的所有 FieldInfo?</fieldinfo> - How to get a System.Collections.Generic.List<FieldInfo> which holds all FieldInfo's of an object of a Type T up to the Object in the class hierarchy? 如何从C#中的FieldInfo获取属性? - How to get properties from FieldInfo in C#? 如何通过 FieldInfo c# 创建数组 - How to create array by FieldInfo c# 如何使用C#将Class对象转换为JSON格式 - How to convert a Class object to JSON Format using C# 如何使用Gembox将Excel函数转换为C#中的实际值 - How to convert an Excel Function to an actual value in C# using Gembox C#,如何将Object转换为Class类型 - C#, How to convert Object to Class Type 如何将 html 元素的 Json object 转换为我可以使用 C# 或 js 或 jquery 在视图上呈现的实际 HTML 元素 - How to convert Json object of html element into actual HTML element that i can render on view using C# or js or jquery C#FieldInfo反射替代 - C# FieldInfo reflection alternatives 将FieldInfo转换为C#中的列表 - Cast FieldInfo to list in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM