如果我有 C# 课程MyClass
如下:
using System.Diagnostics;
namespace ConsoleApplication1
{
class MyClass
{
public int pPublic {get;set;}
private int pPrivate {get;set;}
internal int pInternal {get;set;}
}
class Program
{
static void Main(string[] args)
{
Debug.Assert(typeof(MyClass).GetProperties(
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance).Length == 1);
Debug.Assert(typeof(MyClass).GetProperties(
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance).Length == 2);
// internal?
// protected?
// protected internal?
}
}
}
上面的代码编译运行,没有任何断言失败。 NonPublic 返回内部和私有属性。上似乎没有其他辅助功能类型的标志绑定标志.
如何获取仅包含内部属性的列表/数组?相关说明,但对于我的应用程序来说不是必需的,受保护或受保护内部怎么样?
当您获取房产信息时BindingFlags.NonPublic
,您可以使用以下方法找到 getter 或 setterGetGetMethod(true)
and GetSetMethod(true)
, 分别。然后,您可以检查以下属性(方法信息)以获得准确的访问级别:
-
propertyInfo.GetGetMethod(true).IsPrivate
意味着私人
-
propertyInfo.GetGetMethod(true).IsFamily
意味着受保护
-
propertyInfo.GetGetMethod(true).IsAssembly
意味着内部
-
propertyInfo.GetGetMethod(true).IsFamilyOrAssembly
表示受保护的内部
-
propertyInfo.GetGetMethod(true).IsFamilyAndAssembly
意味着私人保护
类似地对于GetSetMethod(true)
当然。
请记住,让其中一个访问器(getter 或 setter)比另一个访问器受到更多限制是合法的。如果只有一个访问器,则其可访问性就是整个属性的可访问性。如果两个访问器都存在,则most无障碍客房让您可以方便地使用整个酒店。
Use propertyInfo.CanRead
看看是否可以打电话propertyInfo.GetGetMethod
,并使用propertyInfo.CanWrite
看看是否可以打电话propertyInfo.GetSetMethod
. The GetGetMethod
and GetSetMethod
方法返回null
如果访问器不存在(或者如果它是非公开的并且您要求公开的)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)