3

フォーラム/インターネットで、(Public プロパティの)PropetryInfo オブジェクトが Private \ Protected Setter を持っているかどうかを明らかにする方法を検索しました...それはすべて無駄でした....私が見つけたすべての助けは、方法に関するものでしたプライベート セッターを持つパブリック プロパティの "Set" 値...

パブリック プロパティの PropertyInfo オブジェクトがあるかどうかを知りたいのですが、そのセッターが非パブリックかどうかはどうすればわかりますか?

例外処理ブロックで、PropertyInfo オブジェクトの GetValue を実行してから、同じ値を設定して SetValue を呼び出してみましたが、驚いたことに、うまく機能し、エラーが発生しませんでした。

ヘルプは非常に高く評価されます...

例えば

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

クラス終了

どうも

ヴィニ・サンケ。

4

2 に答える 2

7

PropertyInfofor プロパティを取得したら、その関数を呼び出して、set アクセサーGetSetMethodの for を返すことができます。次に、のプロパティをMethodInfoチェックして、set アクセサーがパブリックかどうかを確認できます。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
于 2009-08-27T13:52:56.727 に答える