返回一个类中的所有字段 [英] Return all the fields from a class

查看:39
本文介绍了返回一个类中的所有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的班级中循环遍历字段,将某种类型的字段放入一个列表中,然后返回该列表.我知道我缺少一些关键组件,但我无法弄清楚我做错了什么.这是我目前所拥有的:

I am attempting to cycle through the Fields in my class, place the fields of a certain type into a list and then return that list. I know I am missing some key component but I cannot figure out what I am doing wrong. This is what I have so far:

...

我需要将实际字段的列表作为指针返回,而不是数据的副本.任何帮助使其工作的帮助将不胜感激.

I need to return a list of the actual fields as pointers, not a copy of the data. Any help in getting this to work would be appreciated.

我删除了上面的代码(您应该仍然可以在历史记录中看到它),因为它令人困惑.这是更新后的代码(删除了一些额外的东西),感谢有能力的技术人员的回答:

I removed the above code (you should still be able to see it in the history) because it was confusing. This is the updated code (with some extra stuff removed) that works thanks to the answer from competent_tech:

string prop1;
BaseClass prop2;
SubClass prop3;
SubClass prop4;

public List<BaseClass> GetData()
{
    List<BaseClass> DataList = new List<BaseClass>();

    foreach (System.Reflection.PropertyInfo thisInfo in this.GetType().GetProperties())
    {
        var tempPropery = thisInfo.GetValue(this, null);
        if (tempPropery.GetType().IsSubclassOf(typeof(BaseClass)) || tempPropery.GetType().Equals(typeof(BaseClass)))
        {
            DataList.Add((BaseClass)tempPropery);
        };
    }

    return DataList;
}

上面的代码将允许您从类中获取特定基类型的所有属性并将它们返回到列表中.所以上面的代码会返回 prop2 - prop4.

The above code will allow you to get all the properties of a specific base type from your class and return them in a list. So the above code would return prop2 - prop4.

推荐答案

你可以通过反射来实现.

You can accomplish this through reflection.

foreach (System.Reflection.PropertyInfo oProperty in this.GetType().GetProperties()) {
}

一旦您拥有此信息或过滤属性(例如,通过向要收集的属性添加属性),您可以通过多种方式使用该信息.

There are numerous ways that you can use this information once you have it or filter the properties (for example, by adding an attribute to the properties that you want to collect).

您可以从 MSDN 获取更多信息.

You can get more information from MSDN.

请注意,此代码专门引用了属性,但也有用于检索字段的等效方法(GetFields)和所有成员(GetMembers).

Note that this code specifically references properties, but there are equivalent methods for retrieving fields (GetFields) and all members (GetMembers).

获得 PropertyInfo 后,您可以调用 GetValue 方法:

Once you have the PropertyInfo, you can call the GetValue method:

        foreach (System.Reflection.PropertyInfo oProperty in this.GetType().GetProperties()) {
            Console.WriteLine(oProperty.Name + " = " + oProperty.GetValue(this, null).ToString());
        }

或者,如果您同时查看字段和属性:

Alternatively, if you are looking at both fields and properties:

        foreach (System.Reflection.MemberInfo oMember in this.GetType().GetMembers())
        {
            switch (oMember.MemberType)
            {
                case System.Reflection.MemberTypes.Field:
                    var oField = (System.Reflection.FieldInfo)oMember;
                    Console.WriteLine(oField.Name + " = " + oField.GetValue(this).ToString());
                    break;

                case System.Reflection.MemberTypes.Property:
                    var oProperty = (System.Reflection.PropertyInfo)oMember;
                    Console.WriteLine(oProperty.Name + " = " + oProperty.GetValue(this, null).ToString());
                    break;

            }
        }

这篇关于返回一个类中的所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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