简体   繁体   English

当列表类型未知时将 FieldInfo 值转换为列表

[英]Converting FieldInfo value to a List When List type not known

I have the following:我有以下几点:

    [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;
    }

I create an instance of this and populate it (contents not relevant for question).我创建了一个实例并填充它(内容与问题无关)。 I have a function which takes the instantiated object as one parameter, lets call it module, and the type of this object as the other parameter: module.GetType() .我有一个函数,它将实例化的对象作为一个参数,我们称之为模块,并将此对象的类型作为另一个参数: module.GetType()

This function will then, using reflection, and:然后,此函数将使用反射,并且:

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

The parameter names in the function are Struct and StructType .函数中的参数名称是StructStructType

I loop through the field names within Struct , pull the values and of the different fields and do something with it.我遍历Struct的字段名称,提取不同字段的值并对其进行处理。 All is well until I get to:一切都很好,直到我到达:

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

The function only knows the type of Struct by StructType .该函数仅通过StructType知道StructStructType In VB, the code is simply:在VB中,代码很简单:

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

and then:进而:

    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.但是,在 C# 中,相同的代码不起作用,因为fieldValue是一个对象,我不能在对象上执行fieldValue[0]

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?那么,我的问题是,该函数仅通过StructType知道Struct的类型,如果可能的话,如何在 C# 中复制 VB 行为?

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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM