type.getField在数组字段上返回null

发布于 2025-02-04 08:29:27 字数 1693 浏览 1 评论 0原文

我正在尝试使用反射从类中获取一个数组字段。在一个简单的字段上,它起作用,在数组上没有。

这是

public abstract class Condition : ScriptableObject
{
    public string Name;
    public virtual bool IsVerified() { return false; }
}

public class ExampleScript : MonoBehaviour
{
    [SerializeField] Condition _condition = null;
    [SerializeField] Condition[] _conditions = new Condition[0];
}

[CustomPropertyDrawer(typeof(Condition))]
public class ConditionPropertyDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginProperty(position, label, property);

        Type propertyType = GetPropertyType(property);

        EditorGUI.EndProperty();
    }

    private Type GetPropertyType(SerializedProperty property)
    {
        Type parentType = property.serializedObject.targetObject.GetType();
        Debug.Log($"{parentType} => {property.propertyPath}");
        FieldInfo fi = parentType.GetField(property.propertyPath, BindingFlags.NonPublic | BindingFlags.Instance);
        Debug.Log(fi);
        return fi.FieldType;
    }
}

我获得字段的地方:

Type parentType = property.serializedObject.targetObject.GetType();
Debug.Log($"{parentType} => {property.propertyPath}");
FieldInfo fi = parentType.GetField(property.propertyPath, BindingFlags.NonPublic | BindingFlags.Instance);
Debug.Log(fi);

调试打印(条件var):

示例cliptscript => _条件
myfullnamespace.condition _condition

debug prints(条件[] var):

示例cliptscript => _conditions.Array.data [0]
null

为什么它不返回正确的fieldinfo?
提前致谢

I'm trying to get an array field from class using Reflections. On a simple field it works, on Array it doesn't.

This is the class

public abstract class Condition : ScriptableObject
{
    public string Name;
    public virtual bool IsVerified() { return false; }
}

public class ExampleScript : MonoBehaviour
{
    [SerializeField] Condition _condition = null;
    [SerializeField] Condition[] _conditions = new Condition[0];
}

[CustomPropertyDrawer(typeof(Condition))]
public class ConditionPropertyDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginProperty(position, label, property);

        Type propertyType = GetPropertyType(property);

        EditorGUI.EndProperty();
    }

    private Type GetPropertyType(SerializedProperty property)
    {
        Type parentType = property.serializedObject.targetObject.GetType();
        Debug.Log(
quot;{parentType} => {property.propertyPath}");
        FieldInfo fi = parentType.GetField(property.propertyPath, BindingFlags.NonPublic | BindingFlags.Instance);
        Debug.Log(fi);
        return fi.FieldType;
    }
}

This is where I'm getting Fields:

Type parentType = property.serializedObject.targetObject.GetType();
Debug.Log(
quot;{parentType} => {property.propertyPath}");
FieldInfo fi = parentType.GetField(property.propertyPath, BindingFlags.NonPublic | BindingFlags.Instance);
Debug.Log(fi);

The Debug prints (Condition var):

ExampleScript => _condition
MyFullNameSpace.Condition _condition

The Debug prints (Condition[] var):

ExampleScript => _conditions.Array.data[0]
Null

Why it doesn't return right FieldInfo?
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀞厅☆埖开 2025-02-11 08:29:27

getfield(property.propertypath,...)不起作用,因为它没有路径。它仅采用属性名称。您可以通过将路径划分为点 - 在基数中找到第一个对象,然后在第二个对象第一个中找到属性。对象 ...

请记住,路径警告您使用关键字数组(如果收集到了)。您的路径有class1.class2.array.data [i] .class3之类的东西,所以不要在第2类中搜索名为array的属性。而是将对象施加到iEnumerable并获取i项目

GetField(property.propertyPath, ...) doesnt work, because it does not take a path. It takes only the property-name. You get the property at the end of a path by splitting the pathString at the dots - find the first object in the base - Then the second object in the first object ...

Keep in mind, that the path warns you with the keyword Array, if a collection is coming. Your path is there something like class1.class2.Array.data[i].class3 So dont search in class 2 for a property named Array. Instead cast the object to IEnumerable and get the i item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文