C# Type.GetFields()不';找不到结构字段
我使用反射中的以下行获取对象中的所有字段:C# Type.GetFields()不';找不到结构字段,c#,reflection,C#,Reflection,我使用反射中的以下行获取对象中的所有字段: FieldInfo[] l_Fields = GetType().GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ); 它适用于我的所有字段,但完全忽略此字段: private OffSet m_Offest; “偏移量”是一个结构,如下所示: public struct OffSet { public float x; publ
FieldInfo[] l_Fields = GetType().GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
它适用于我的所有字段,但完全忽略此字段:
private OffSet m_Offest;
“偏移量”是一个结构,如下所示:
public struct OffSet
{
public float x;
public float y;
}
此字段不在返回的数组中,是否有方法获取它
下面是类的定义。我得到了最后两个函数中的字段
[Serializable]
public abstract class AITreeNode : ISerializable
{
//>----------------------------------------------------------------------------------------
// STRUCT
//-----------------------------------------------------------------------------------------
public struct OffSet
{
public float x;
public float y;
}
//>----------------------------------------------------------------------------------------
// ENUM
//-----------------------------------------------------------------------------------------
public enum Status
{
None,
Available,
Unavailable,
Active,
Success,
Failed
}
//>-----------------------------------------------------------------------------------------
// VARIABLES
//------------------------------------------------------------------------------------------
public String Caption;
public bool Enabled = true;
// Display
private OffSet m_Offest;
// Non Serialized data
[NonSerialized] protected AITree m_AITreeAT;
[NonSerialized] protected AITreeBranch m_BranchATB;
[NonSerialized] protected Status m_LastStatusS = Status.None;
[NonSerialized] protected bool m_IsActiveB;
//>-----------------------------------------------------------------------------------------
// GETTERS
//------------------------------------------------------------------------------------------
public AITreeBranch GetBranchATB() { return m_BranchATB; }
public bool IsActive() { return m_IsActiveB; }
public Status LastStatusS() { return m_LastStatusS; }
//>-----------------------------------------------------------------------------------------
// SETTERS
//------------------------------------------------------------------------------------------
public void SetBranch ( AITreeBranch _BranchATB ) { m_BranchATB = _BranchATB; }
public void SetAITree ( AITree _TreeAT ) { m_AITreeAT = _TreeAT; }
//>-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
public float X
{ get { return m_Offest.x; } set { m_Offest.x = value; } }
//>-----------------------------------------------------------------------------------------
public float Y
{ get { return m_Offest.y; } set { m_Offest.y = value; } }
//>-----------------------------------------------------------------------------------------
//>-----------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
public AITreeNode()
{
m_Offest.y = -5;
}
#region Serialization
//>-----------------------------------------------------------------------------------------
// The special constructor is used to deserialize values.
// Every class inheriting from AITreeNode needs to implement a constructor with such parameters
//------------------------------------------------------------------------------------------
public AITreeNode( SerializationInfo info, StreamingContext context )
{
// Reset the property value using the GetValue method.
FieldInfo[] l_FieldsAFI = GetType().GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
foreach ( FieldInfo fieldInfo in l_FieldsAFI )
{
if ( fieldInfo.IsNotSerialized ) continue;
try
{
fieldInfo.SetValue( this, info.GetValue( fieldInfo.Name, fieldInfo.FieldType ) );
}
catch
{
UnityEngine.Debug.Log( "Field " + fieldInfo.Name + " is new. Default value is used" );
}
}
}
//>-----------------------------------------------------------------------------------------
// Implement this method to serialize data. The method is called on serialization.
//------------------------------------------------------------------------------------------
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// Use the AddValue method to specify serialized values.
FieldInfo[] l_FieldsAFI = GetType().GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
foreach ( FieldInfo fieldInfo in l_FieldsAFI )
{
if ( fieldInfo.IsNotSerialized )
{
UnityEngine.Debug.Log( "Non serialized Field " + fieldInfo.Name );
continue;
}
info.AddValue( fieldInfo.Name, fieldInfo.GetValue( this ), fieldInfo.FieldType );
UnityEngine.Debug.Log( "Saving Field " + fieldInfo.Name );
}
}
... rest of the class
}
我通过将m_Offset的可访问性从“private”更改为“protected”,解决了这个问题 问题是我从一个子类调用了GetFields()函数,而这个类无法访问基类的私有字段 我没有使用BaseType.GetFields()方法,因为我还需要获取子类的字段,而不仅仅是BaseType的字段
谢谢你的回答正如我所预料的,问题在于继承。如果在基类中声明了私有字段,则不会返回该字段。您需要使用
Type.BaseType
来获取该信息
这是你怎么做的
var typeHierarchies = new List<Type>();
var type = this.GetType();
while(type.BaseType != null)
{
typeHierarchies.Add(type);
type = type.BaseType;
}
FieldInfo[] l_Fields = typeHierarchies.SelectMany( x=>x.GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)).ToArray();
var typeHierarchies=new List();
var type=this.GetType();
while(type.BaseType!=null)
{
类型层次结构。添加(类型);
type=type.BaseType;
}
FieldInfo[]l_Fields=typeHierarchies.SelectMany(x=>x.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)).ToArray();
您试图从哪种类型获取字段?发布该类型定义。我正在尝试从自定义类获取字段。。。把它完全放在这里有点大。此类包含多种类型的字段(float、int等,以及其他自定义类)。所有这些字段都在数组中返回,除了我的字段m_Offset。你需要什么样的信息?这可能是唯一的私有字段吗?发布一些它返回FieldInfo的字段和它不返回的Offest字段。它们都存在于同一个类中,还是有继承关系?一个简短但完整的程序对我们很有帮助。@Matíasfidemrazer你基本上说‘是的,你做错了什么事’。这是一个如何有用的答案?我不确定我是否理解这里的情况。。。为什么我在发布问题的解决方案时两次被否决?甚至我的问题本身也被否决了。我以前在不知道问题原因的情况下问问题是错误的吗?