反思:如何从属性信息对象中查找该属性是否具有非公共(私有/受保护)Setter?
我在论坛/互联网上搜索了解决方案,PropetryInfo 对象(公共财产)如何揭示它是否有 Private \ Protected Setter ...这一切都是徒劳的......我发现的所有帮助都是关于如何具有私有 Setter 的公共属性的“设置”值...
我想知道我是否有公共属性的 PropertyInfo 对象,我如何知道它的 Setter 是否是非公共的?
我尝试在异常处理块中执行 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
End Class
Thx
Vinit sankhe。
I searched on the forum / Internet for the solution how a PropetryInfo object (of a Public property) can reveal if it has a Private \ Protected Setter ... it was all in vain .... all help I found was about how to "Set" value of a public property having a Private Setter...
I would like to know if I have a PropertyInfo object of a public property, how would I know if its Setter is Non Public?
I tried, in a exception handling block, where I did a GetValue of the PropertyInfo object and then called SetValue by setting the same value back... but to my surprise it worked well and didn error out.
Help would be very much approaciated...
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
End Class
Thx
Vinit sankhe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获得
PropertyInfo
对于该属性,您可以调用其GetSetMethod
函数返回MethodInfo
< /a> 用于设置访问器。然后,您可以检查MethodInfo
的IsPublic
属性来查看设置的访问器是否是公共的。Once you have the
PropertyInfo
for the property, you can call itsGetSetMethod
function to return theMethodInfo
for the set accessor. You can then check theMethodInfo
'sIsPublic
property to see if the set accessor is public.其实,那么你就分别取一个位置给set和get方法,下面是一个例子:
End Class
PropertyInfo.GetAccessors方法返回一个get和set方法的数组。
In fact, then you both take a position to set and get method, here is an example:
End Class
PropertyInfo.GetAccessors the method returns a array of get and set the method.