jeudi 24 février 2022

Get only the parent fields from an inherited class

I have a class called Stats

public class Stats : ScriptableObject
{
    public double vitality;   
    public double stamina;     
    public double endurance; 
} 

I also have a class that inherits Stats

public class Equipment : Stats
{
    public string name;
    // other fields that define equipment
} 

I want to be able to get all the fields from the instance of Stats that is inherited from Equipment

So I added this in an Interface class

    public void AddStats(Equipment equippedItem)
    {
        Stats stats = equippedItem as Stats;
        if (stats != null)
        {
            GetPropertyValues(stats);
        }
    }
   public static void GetPropertyValues(System.Object obj)
   {
        Type t = obj.GetType();
        FieldInfo[] fis = t.GetFields(BindingFlags.Instance | BindingFlags.Public);
        foreach (var fieldInfo in fis)
                Debug.Log(fieldInfo.FieldType + " " + fieldInfo.Name + " " + fieldInfo.GetValue(obj));
   }

The issue is that it is getting all the fields from the Equipment as well.

How could I make it so that only gets the fields from Stats?





Aucun commentaire:

Enregistrer un commentaire