type.getField在数组字段上返回null
我正在尝试使用反射从类中获取一个数组字段。在一个简单的字段上,它起作用,在数组上没有。
这是
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 theproperty-name
. You get the property at the end of a path by splitting the pathString at the dots - find thefirst object
in the base - Then thesecond object
in thefirst object
...Keep in mind, that the path
warns
you with the keyword Array, if a collection is coming. Your path is there something likeclass1.class2.Array.data[i].class3
So dont search in class 2 for a property namedArray
. Instead cast the object toIEnumerable
and get thei item
.