字段信息值转换为列表当不知道列表类型 [英] Converting FieldInfo value to a List When List type not known

查看:149
本文介绍了字段信息值转换为列表当不知道列表类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

    [Serializable()]
    public struct ModuleStruct {
        public string moduleId;
        public bool isActive;
        public bool hasFrenchVersion;
        public string titleEn;
        public string titleFr;
        public string descriptionEn;
        public string descriptionFr;
        public bool isLoaded;
        public List<SectionStruct> sections;
        public List<QuestionStruct> questions;
    }



我创造了这样的一个实例,填充它(的内容无关的问题) 。我有一个函数,该函数实例化对象作为一个参数,让我们叫它模块,这个对象与其他参数的类型: module.GetType()

这功能的话,使用反射和:

This function will then, using reflection, and:

    FieldInfo[] fields = StructType.GetFields();
    string fieldName = string.Empty;

在函数中的参数名称结构体 StructType

The parameter names in the function are Struct and StructType.

通过字段名我环路结构内,拉的价值观和不同的领域,并用它做什么。一切都很好,直到我去:

I loop through the field names within Struct, pull the values and of the different fields and do something with it. All is well until I get to:

    public List<SectionStruct> sections;
    public List<QuestionStruct> questions;



该功能只知道结构体的类型通过 StructType 。在VB中,代码很简单:

The function only knows the type of Struct by StructType. In VB, the code is simply:

    Dim fieldValue = Nothing
    fieldValue = fields(8).GetValue(Struct)

和则:

    fieldValue(0)

要获得在清单部分中的第一个元素;然而,在C#中,同样的代码不起作用,因为 fieldValue方法是一个对象,我不能做 fieldValue方法[0] 上的对象。

to get the first element in the list sections; however, in C#, the same code does not work because fieldValue is an object and I cannot do fieldValue[0] on an object.

我的问题的话,是因为该功能只能通过结构体的类型> StructType ,我怎么复制VB行为在C#中,如果它甚至有可能?

My question, then, is given that the function only knows the type of Struct by StructType, how do I replicate the VB behaviour in C#, if it is even possible?

推荐答案

以下是这几乎是拼了一些(很简单)示例代码...我真的不想做整个事情给你,因为这可能是一个反射很大的教训:)

Here are some (very simple) example code that's pretty much spelled out... I really don't want to do the whole thing for you, because this could be a great lesson in reflection :)

private void DoSomethingWithFields<T>(T obj)
{
    // Go through all fields of the type.
    foreach (var field in typeof(T).GetFields())
    {
        var fieldValue = field.GetValue(obj);

        // You would probably need to do a null check
        // somewhere to avoid a NullReferenceException.

        // Check if this is a list/array
        if (typeof(IList).IsAssignableFrom(field.FieldType))
        {
            // By now, we know that this is assignable from IList, so we can safely cast it.
            foreach (var item in fieldValue as IList)
            {
                // Do you want to know the item type?
                var itemType = item.GetType();

                // Do what you want with the items.
            }
        }
        else
        {
            // This is not a list, do something with value
        }
    }
}

这篇关于字段信息值转换为列表当不知道列表类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆