如何检查财产设定者是否公开 [英] How to check if property setter is public
本文介绍了如何检查财产设定者是否公开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出一个PropertyInfo对象,如何检查该属性的设置程序是否公开?
Given a PropertyInfo object, how can I check that the setter of the property is public?
推荐答案
检查得到的内容从 GetSetMethod
:
Check what you get back from GetSetMethod
:
MethodInfo setMethod = propInfo.GetSetMethod();
if (setMethod == null)
{
// The setter doesn't exist or isn't public.
}
或者,对理查德的答案:
if (propInfo.CanWrite && propInfo.GetSetMethod(/*nonPublic*/ true).IsPublic)
{
// The setter exists and is public.
}
请注意,如果您只想设置一个属性,有一个塞特犬,您实际上不必关心塞特犬是否是公开的。您可以使用它,公共或私有:
Note that if all you want to do is set a property as long as it has a setter, you don't actually have to care whether the setter is public. You can just use it, public or private:
// This will give you the setter, whatever its accessibility,
// assuming it exists.
MethodInfo setter = propInfo.GetSetMethod(/*nonPublic*/ true);
if (setter != null)
{
// Just be aware that you're kind of being sneaky here.
setter.Invoke(target, new object[] { value });
}
这篇关于如何检查财产设定者是否公开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文