Get private Properties/Method of base-class with reflection

前端 未结 2 1547
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 09:57

With Type.GetProperties() you can retrieve all properties of your current class and the public properties of the base-class. Is it somehow possible

相关标签:
2条回答
  • 2020-12-03 10:36

    Iterate through the base types (type = type.BaseType), until type.BaseType is null.

    MethodInfo mI = null;
    Type baseType = someObject.GetType();
    while (mI == null)
    {
        mI = baseType.GetMethod("SomePrivateMethod", BindingFlags.NonPublic | BindingFlags.Instance);
        baseType = baseType.BaseType;
        if (baseType == null) break;
    }
    mI.Invoke(someObject, new object[] {});
    
    0 讨论(0)
  • 2020-12-03 10:42

    To get all properties (public + private/protected/internal, static + instance) of a given Type someType (maybe using GetType() to get someType):

    PropertyInfo[] props = someType.BaseType.GetProperties(
            BindingFlags.NonPublic | BindingFlags.Public
            | BindingFlags.Instance | BindingFlags.Static)
    
    0 讨论(0)
提交回复
热议问题