开发者

How can I access a "private virtual" property starting with Underscore through Reflection

开发者 https://www.devze.com 2023-03-20 10:49 出处:网络
I was trying to access the following property using Reflection because I don\'t have the original source code (suppose this was decompiled through Reflector). It seems that something is special about

I was trying to access the following property using Reflection because I don't have the original source code (suppose this was decompiled through Reflector). It seems that something is special about it being "private virtual" or maybe because it has "_" in the beginning of the property. I can access all the other private properties no problem except this one. Just can't figure out what I am doing wro开发者_如何学Pythonng:

 private virtual String _MyProperty
    {
      get
      {
        return this.__MyProperty;
      }
      [MethodImpl(MethodImplOptions.Synchronized)] set
      {
        this.__MyProperty = "blah";
      }
    }

I tried to access it using Reflection as following:

ReflectionHelper.GetPropertyValue(<class of above property>, "_MyProperty")

Using the following method in my ReflectionHelper class:

Public Shared Function GetPropertyValue(ByVal obj As Object, ByVal prop As String) As Object
        Dim objectType As Type = obj.GetType()
        Dim bindingFlags As BindingFlags = bindingFlags.GetProperty Or bindingFlags.Public Or bindingFlags.NonPublic Or bindingFlags.Instance

        Dim propInfo As PropertyInfo = objectType.GetProperty(prop, bindingFlags)
        If propInfo Is Nothing Then
            Throw New Exception("Property: '" & prop & "' not found in: " & objectType.ToString)
        End If
        Return propInfo.GetValue(obj, Nothing)
    End Function


private virtual String _MyProperty should be a compiler error. I get both "A private method cannot be polymorphic" and "A virtual method cannot be private" when I try this in VS 2010.

This makes sense, because private means "cannot be accessed outside of this class (including by derived classes)", and virtual means "can be overridden by derived classes". If you could override the method in a derived class, then you could call it from the derived class, making it no longer private.

I wonder if you were decompling some code that was written in managed C++. I think the following is possible in Visual C++:

interface class I
{
  property bool IsOk
  {
    bool get();
  }
};

public ref class A abstract : I
{
private:
  virtual property bool IsFine
  {
    bool get() sealed = I::IsOk::get
    {
      return false;
    }
  }
};

Edit: I found a bug report that mentioned this as a bug in the Visual C++ compiler: https://connect.microsoft.com/VisualStudio/feedback/details/651255/c-cli-property-overriding-and-renaming.


Ok I figured it out. Going By Vlad's question below the original post I changed the ReflectionHelper function "LookThroughAllBaseProperties" as following:

Public Shared Function GetPropertyValue(ByVal obj As Object, ByVal prop As String, Optional ByVal bindFlags As BindingFlags = BindingFlags.GetProperty Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance) As Object
        Dim objectType As Type = obj.GetType()

        Dim propInfo As PropertyInfo = objectType.GetProperty(prop, bindFlags)
        If propInfo Is Nothing Then
            propInfo = LookThroughtAllBaseProperties(objectType, prop, bindFlags)
        End If

        If propInfo Is Nothing Then
            Throw New Exception("Property: '" & prop & "' not found in: " & objectType.ToString)
        End If
        Return propInfo.GetValue(obj, Nothing)
    End Function

    Private Shared Function LookThroughtAllBaseProperties(ByVal objectType As Type, ByVal name As String, ByVal bindFlags As BindingFlags) As PropertyInfo
        Dim objType As Type = objectType
        While objType IsNot Nothing
            For Each availProp As PropertyInfo In objType.GetProperties(bindFlags)
                If availProp.Name = name Then
                    Return availProp
                End If
            Next
            objType = objType.BaseType
        End While
        Return Nothing
    End Function

It now finds the property. Maybe now someone can explain why this works...

0

精彩评论

暂无评论...
验证码 换一张
取 消