2009-08-27 11 views
3

我在論壇/互聯網上搜索瞭解決方案PropetryInfo對象(公共屬性)如何顯示它是否具有Private \ Protected Setter ......這一切都是徒勞的......我發現的所有幫助都是關於如何「設置」具有私人安裝人員的公共財產的價值...反思:如果該屬性有非公共(私人/受保護)設置者,如何從屬性信息對象中查找?

我想知道如果我有一個PropertyInfo對象的公共屬性,我怎麼知道它的Setter是否爲非公開?

我試着在一個異常處理塊中執行PropertyInfo對象的GetValue,然後通過設置相同的值來調用SetValue ......但令我驚訝的是它運行良好並沒有出錯。

幫助將非常認真...

E.g.

Public Class Class1 
Public Property HasContextChanged() As Boolean 
    Get 
     Return _hasContextChanged 
    End Get 
    Protected Set(ByVal value As Boolean) 
     _hasContextChanged = value 
    End Set 
End Property 

Public Function CanWriteProperty(Optional ByVal propName As String = "HasContextChanged") As Boolean 
    Dim prInfo As PropertyInfo = Me.GetType.GetProperty(propName) 
    If prInfo Is Nothing Then Return False 
    If Not prInfo.CanWrite Then 
     Return False 
    Else 
     Try 
      Dim value As Object = prInfo.GetValue(ownObj, Nothing) 
      prInfo.SetValue(ownObj, value, Nothing) 'Works for Private Setter 
     Catch ex As Exception 
      Return False 'Not coming here whatsoever 
     End Try 
    End If 
    Return True 
End Function 

末級

THX

攝影指導Vinit sankhe。

+0

請在標籤中指定編程語言:) – 2009-08-27 13:12:04

回答

7

一旦你有PropertyInfo的屬性,你可以調用它的GetSetMethod函數返回MethodInfo設置存取器。然後,您可以檢查MethodInfoIsPublic屬性以查看設置訪問器是否公開。

Dim prInfo As PropertyInfo = Me.GetType.GetProperty(propName) 
Dim method as MethodInfo = prInfo.GetSetMethod() 
If Not method.IsPublic Then 
    Return False 
Else 
    Dim value As Object = prInfo.GetValue(ownObj, Nothing) 
    prInfo.SetValue(ownObj, value, Nothing) 'Works for Private Setter 
End If 
+0

Thx man ...你讓我的一天... :-) – 2009-08-27 15:07:41

+0

非常翔實,儘管MSDN(http://msdn.microsoft.com/en-us/ library/2ef4d5h3.aspx)表示只會返回MethodInfo「如果set訪問器存在並且是公共的」。在這種情況下,我們可能需要使用重載(http://msdn.microsoft.com/en-us/library/scfx0019.aspx),它使用bool指示是否檢索非公共設置者。 – 2009-09-15 15:14:15

+0

我可以驗證GetSetMethod()只會返回MethodInfo,如果訪問存在並且在.NET 3.5中是公共的。 好問題和很好的答案。感謝大家! – Eric 2010-04-07 21:52:32

0

事實上,那你們倆坐的位置來設置和獲取方法,這裏有一個例子:

Public Class ExPropertyInfo 
Inherits PropertyInfo 

Dim p As PropertyInfo 

Public Sub New(ByVal [property] As PropertyInfo) 
    p = [property] 
    Dim accssors() As MethodInfo = [property].GetAccessors(True) 
    Select Case accssors.Length 
     Case 1 
      isassembly_ = accssors(0).IsAssembly 
      isfamily_ = accssors(0).IsFamily 
      isprivate_ = accssors(0).IsPrivate 
      ispublic_ = accssors(0).IsPublic 
      isstatic_ = accssors(0).IsStatic 
      isfamilyandassembly_ = accssors(0).IsFamilyAndAssembly 
      isfamilyorassembly_ = accssors(0).IsFamilyOrAssembly 
     Case 2 
      Dim method As MethodInfo = Nothing 
      If accssors(0).IsPrivate Then 
       If accssors(1).IsFamily Or accssors(1).IsFamilyAndAssembly Or accssors(1).IsFamilyOrAssembly Or accssors(1).IsAssembly Or accssors(1).IsPublic Then 
        method = accssors(1) 
       Else 
        method = accssors(0) 
       End If 
      ElseIf accssors(0).IsFamily Then 
       If accssors(1).IsFamilyAndAssembly Or accssors(1).IsFamilyOrAssembly Or accssors(1).IsAssembly Or accssors(1).IsPublic Then 
        method = accssors(1) 
       Else 
        method = accssors(0) 
       End If 
      ElseIf accssors(0).IsFamilyAndAssembly Or accssors(0).IsFamilyOrAssembly Then 
       If accssors(1).IsAssembly Or accssors(1).IsPublic Then 
        method = accssors(1) 
       Else 
        method = accssors(0) 
       End If 
      ElseIf accssors(0).IsAssembly Or accssors(0).IsPublic Then 
       If accssors(1).IsPublic Then 
        method = accssors(1) 
       Else 
        method = accssors(0) 
       End If 
      End If 
      isassembly_ = method.IsAssembly 
      isfamily_ = method.IsFamily 
      isprivate_ = method.IsPrivate 
      ispublic_ = method.IsPublic 
      isstatic_ = method.IsStatic 
      isfamilyandassembly_ = method.IsFamilyAndAssembly 
      isfamilyorassembly_ = method.IsFamilyOrAssembly 
    End Select 
End Sub 

Public Overrides ReadOnly Property Attributes As Reflection.PropertyAttributes 
    Get 
     Return p.Attributes 
    End Get 
End Property 

Public Overrides ReadOnly Property CanRead As Boolean 
    Get 
     Return p.CanRead 
    End Get 
End Property 

Public Overrides ReadOnly Property CanWrite As Boolean 
    Get 
     Return p.CanWrite 
    End Get 
End Property 

Public Overrides ReadOnly Property DeclaringType As Type 
    Get 
     Return p.DeclaringType 
    End Get 
End Property 

Public Overloads Overrides Function GetAccessors(nonPublic As Boolean) As MethodInfo() 
    Return p.GetAccessors(nonPublic) 
End Function 

Public Overloads Overrides Function GetCustomAttributes(inherit As Boolean) As Object() 
    Return p.GetCustomAttributes(inherit) 
End Function 

Public Overloads Overrides Function GetCustomAttributes(attributeType As Type, inherit As Boolean) As Object() 
    Return p.GetCustomAttributes(attributeType, inherit) 
End Function 

Public Overloads Overrides Function GetGetMethod(nonPublic As Boolean) As MethodInfo 
    Return p.GetGetMethod(nonPublic) 
End Function 

Public Overrides Function GetIndexParameters() As ParameterInfo() 
    Return p.GetIndexParameters 
End Function 

Public Overloads Overrides Function GetSetMethod(nonPublic As Boolean) As MethodInfo 
    Return p.GetSetMethod(nonPublic) 
End Function 

Public Overloads Overrides Function GetValue(obj As Object, invokeAttr As BindingFlags, binder As Binder, index() As Object, culture As Globalization.CultureInfo) As Object 
    Return p.GetValue(obj, invokeAttr, binder, index, culture) 
End Function 

Public Overrides Function IsDefined(attributeType As Type, inherit As Boolean) As Boolean 
    Return p.IsDefined(attributeType, inherit) 
End Function 

Public Overrides ReadOnly Property Name As String 
    Get 
     Return p.Name 
    End Get 
End Property 

Public Overrides ReadOnly Property PropertyType As Type 
    Get 
     Return p.PropertyType 
    End Get 
End Property 

Public Overrides ReadOnly Property ReflectedType As Type 
    Get 
     Return p.ReflectedType 
    End Get 
End Property 

Public Overloads Overrides Sub SetValue(obj As Object, value As Object, invokeAttr As BindingFlags, binder As Binder, index() As Object, culture As Globalization.CultureInfo) 
    p.SetValue(obj, value, invokeAttr, binder, index, culture) 
End Sub 

Private ispublic_ As Boolean 
Public ReadOnly Property IsPublic As Boolean 
    Get 
     Return ispublic_ 
    End Get 
End Property 

Private isprivate_ As Boolean 
Public ReadOnly Property IsPrivate As Boolean 
    Get 
     Return isprivate_ 
    End Get 
End Property 

Private isassembly_ As Boolean 
Public ReadOnly Property IsAssembly As Boolean 
    Get 
     Return isassembly_ 
    End Get 
End Property 

Private isfamily_ As Boolean 
Public ReadOnly Property IsFamily As Boolean 
    Get 
     Return isfamily_ 
    End Get 
End Property 

Private isstatic_ As Boolean 
Public ReadOnly Property IsStatic As Boolean 
    Get 
     Return isstatic_ 
    End Get 
End Property 

Private isfamilyandassembly_ As Boolean 
Public ReadOnly Property IsFamilyAndAssembly As Boolean 
    Get 
     Return isfamilyandassembly_ 
    End Get 
End Property 

Private isfamilyorassembly_ As Boolean 
Public ReadOnly Property IsFamilyOrAssembly As Boolean 
    Get 
     Return isfamilyorassembly_ 
    End Get 
End Property 

末級

Dim [property] As ExPropertyInfo = New ExPropertyInfo(GetType(Form1).GetProperty("abc", BindingFlags.NonPublic Or BindingFlags.Static)) 

PropertyInfo.GetAccessors方法返回一個獲取並設置方法的數組。

相關問題