Return all fields from a class

I am trying to loop through the fields in my class, put fields of a certain type in a list, and then return that list. I know I am missing a 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 actual fields as pointers, not a copy of the data. Any help getting this to work would be appreciated.

I removed the above code (you can still see it in history) because it was confusing. This is the updated code (with some additional removal) 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;
}

      

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

+3


source to share


2 answers


You can do this through reflection.

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

      

There are many ways to use this information after using it or filtering properties (for example, by adding an attribute to the properties you want to collect).

You can get more information from MSDN .

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



Once you have 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;

            }
        }

      

+2


source


You can store data in a dictionary. For each property, just set / get the value from the dictionary. Something like that:

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

      



One day I found this: "Enum.GetValues ​​(typeof (TvStations))", maybe you should look for some similar ones.

0


source







All Articles