日期:2014-05-18  浏览次数:20944 次

C# 中创建一个对象,问一下这个对象所有的成员是什么?谢谢C#高手
C# 中创建一个对象,问一下这个对象所有的成员是什么?谢谢C#高手。

public class c
{
}
c obj = new c()

obj对象是一个空类(类里面什么也没有)实例化而来的。但我相信除了继承自System.Object的成员之外。微软肯定还对这个对象添加了不少私有成员(只是不暴露接口而已,这可能涉及.net framwork底层)。怎样获得这些私有成员。还有,他们各有什么意义。

------解决方案--------------------
C# code

[Serializable, ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)]
public class Object
{
    // Methods
    public virtual bool Equals(object obj)
    {
        return InternalEquals(this, obj);
    }

    public static bool Equals(object objA, object objB)
    {
        return ((objA == objB) || (((objA != null) && (objB != null)) && objA.Equals(objB)));
    }

    private void FieldGetter(string typeName, string fieldName, ref object val)
    {
        val = this.GetFieldInfo(typeName, fieldName).GetValue(this);
    }

    private void FieldSetter(string typeName, string fieldName, object val)
    {
        FieldInfo fieldInfo = this.GetFieldInfo(typeName, fieldName);
        if (fieldInfo.IsInitOnly)
        {
            throw new FieldAccessException(Environment.GetResourceString("FieldAccess_InitOnly"));
        }
        Message.CoerceArg(val, fieldInfo.FieldType);
        fieldInfo.SetValue(this, val);
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    protected override void Finalize()
    {
    }

    private FieldInfo GetFieldInfo(string typeName, string fieldName)
    {
        Type baseType = this.GetType();
        while (baseType != null)
        {
            if (baseType.FullName.Equals(typeName))
            {
                break;
            }
            baseType = baseType.BaseType;
        }
        if (baseType == null)
        {
            throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), new object[] { typeName }));
        }
        FieldInfo field = baseType.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
        if (field == null)
        {
            throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadField"), new object[] { fieldName, typeName }));
        }
        return field;
    }

    public virtual int GetHashCode()
    {
        return InternalGetHashCode(this);
    }

    [MethodImpl(MethodImplOptions.InternalCall)]
    public extern Type GetType();
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern bool InternalEquals(object objA, object objB);
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern int InternalGetHashCode(object obj);
    [MethodImpl(MethodImplOptions.InternalCall)]
    protected extern object MemberwiseClone();
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    public static bool ReferenceEquals(object objA, object objB)
    {
        return (objA == objB);
    }

    public virtual string ToString()
    {
        return this.GetType().ToString();
    }
}

------解决方案--------------------
obj.GetType().BaseType.GetType().GetProperties()