Popular Posts

Thursday, July 12, 2012

.net - Is it possible to set private property via reflection - Stack Overflow

Once the class is published then we don't have any control on how it will be used,,,, even we can set the Private or protected members of the class using refelection... see below


  Private Sub SetPrivateProperty(Of t)(
                                             ByVal targertObject As t,
                                             ByVal targetPropertyName As String,
                                             ByVal targetPropertyValue As Object
                                         )
        Dim type As Type = targertObject.GetType
        type.InvokeMember(targetPropertyName,
                          BindingFlags.NonPublic Or
                          BindingFlags.SetProperty Or
                          BindingFlags.Instance, Nothing,
                          targertObject, New Object() {targetPropertyValue})
    End Sub


.net - Is it possible to set private property via reflection - Stack Overflow:

'via Blog this'

No comments:

Post a Comment