返回类中的所有字段

时间:2013-01-19 01:26:50

标签: c# reflection

我正在尝试遍历我班级中的Fields,将某种类型的字段放入列表中,然后返回该列表。我知道我错过了一些关键组件,但我无法弄清楚我做错了什么。这就是我到目前为止所做的:

...

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

我删除了上面的代码(您仍然可以在历史记录中看到它),因为它令人困惑。这是更新的代码(删除了一些额外的东西),这得益于来自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。

2 个答案:

答案 0 :(得分:2)

你可以通过反思来实现这一目标。

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

有许多方法可以在获取或过滤属性后使用此信息(例如,通过向要收集的属性添加属性)。

您可以获得更多信息from MSDN

请注意,此代码专门引用属性,但是有相同的方法可以检索字段(GetFields)和所有成员(GetMembers)。

拥有PropertyInfo后,您可以调用GetValue方法:

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

或者,如果您正在查看字段和属性:

        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;

            }
        }

答案 1 :(得分:0)

您可以在Dictionary中存储数据。对于每个属性,只从字典中设置/获取值。 像这样:

public class MyData {
    private Dictionary<string, object> myData;
    public MyData {
        this.myData = new Dictionary<string, object>();
    }

    public decimal LastVersion { get { return (decimal)this.myData["LastVersion"]; } private set { this.myData["LastVersion"] = value; } }

    public PrimaryAbility STR { get { return (PrimaryAbility)this.myData["STR"]; } private set { this.myData["STR"] = value; } }
    public PrimaryAbility DEX { get { return (PrimaryAbility)this.myData["DEX"]; } private set { this.myData["DEX"] = value; } }
    public PrimaryAbility CON { get { return (PrimaryAbility)this.myData["CON"]; } private set { this.myData["CON"] = value; } }
    public PrimaryAbility INT { get { return (PrimaryAbility)this.myData["INT"]; } private set { this.myData["INT"] = value; } }
    public PrimaryAbility WIS { get { return (PrimaryAbility)this.myData["WIS"]; } private set { this.myData["WIS"] = value; } }
    public PrimaryAbility CHA { get { return (PrimaryAbility)this.myData["CHA"]; } private set { this.myData["CHA"] = value; } }

    public DerivedAbility HP { get { return (DerivedAbility)this.myData["HP"]; } private set { this.myData["HP"] = value; } }
    public DerivedAbility MP { get { return (DerivedAbility)this.myData["MP"]; } private set { this.myData["MP"] = value; } }
    public DerivedAbility SP { get { return (DerivedAbility)this.myData["SP"]; } private set { this.myData["SP"] = value; } }
    public DerivedAbility AC { get { return (DerivedAbility)this.myData["AC"]; } private set { this.myData["AC"] = value; } }
}

有一天我发现了这个:&#34; Enum.GetValues(typeof(TvStations))&#34;,也许你应该搜索一下这样的。