仅获取继承类的父字段

问题描述 投票:0回答:2

我有一门叫做统计的课程

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

我还有一个继承Stats的类

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

我希望能够从继承自 Equipment 的 Stats 实例中获取所有字段

所以我将其添加到接口类中

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

问题是它也从设备中获取所有字段。

我怎样才能做到只从统计数据中获取字段?

c# unity-game-engine reflection
2个回答
0
投票

请勿使用

Type t = obj.GetType();

但是传入你的目标类型

typeof(Stats);

对于这个用例,您可能会出现过载,例如

public static void GetPropertyValues(System.Object obj, Type type)

并添加检查给定的

obj
是否属于类型
type
或子类

var objType = obj.GetType();
if(objType != type && !objType.IsSubclassOf(type))
{
    throw ArgumentException($"Provided object of type {objType.Name} is not derived from {type.Name}");
}

或与通用药相同

public static void GetPropertyValues<Ttype>(System.Object obj)

var objType = obj.GetType();
var type = typeof(T);
if(objType != type && !objType.IsSubclassOf(type))
{
    throw ArgumentException($"Provided object of type {objType.Name} is not derived from {type.Name}");
}

反射永远不会“美丽”;)


0
投票

我在这里找到了解决方案如何在 C# 中使用反射获取自定义方法列表 所以我创建了一个名为 stat 的属性

[AttributeUsage(AttributeTargets.Field)]
public class Stat : Attribute {}

我将此属性添加到我想要在 Stats 类中获取的所有变量中:

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

然后我更新了

GetPropertyValues
方法来获取属性并将其添加到数组中。

   public static void GetPropertyValues(System.Object obj, double[] array)
   {
        // Getting the class
        Type t = obj.GetType();
        // Getting all the fields(Variables)
        FieldInfo[] fis = t.GetFields(BindingFlags.ExactBinding | BindingFlags.Instance | BindingFlags.Public);

        int arrayLength = 0;
        // Iterating over all fields (variables)
        for (int i = 0; i < fis.Length; i++) 
        {
            // Filtering through the list and if the field(Variable) is marked with a Stat attribute adds it to array length
            arrayLength += i;
        }

        array = new double[arrayLength];

        // Iterating over all the fields (variables)
        for (int i = 0; i < fis.Length; i++) 
        {
            // Filtering through the list and if the field(Variable) is marked with a Stat attribute ....
            Stat attribute = Attribute.GetCustomAttribute(fis[i], typeof(Stat)) as Stat;

            if (attribute != null)
            {
                Debug.Log(fis[i].Name + " " + fis[i].GetValue(obj)); // The name of the flagged variable.
                array[i] = (Convert.ToDouble(fis[i].GetValue(obj)));
                Debug.Log(array[i]);
            }
        }
   }
© www.soinside.com 2019 - 2024. All rights reserved.