如果我有一个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?
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面编译的代码是没有任何断言失败的运行.NonPublic返回内部和私有属性.BindingFlags上的其他辅助功能类型似乎没有标志.
如何获取仅包含内部属性的列表/数组?在相关的说明中,但对我的应用程序来说不是必需的,那么受保护或受保护的内部呢?
有人可以解释一下,为什么 System.Type 中的 GetProperty 方法对于声明为“内部”但适用于“公共”的属性返回 null。
internal class Test{
public string ALocal { get; set; }
internal string SLocal { get; set; }}
var test = new Test();
var testType = test.GetType();
var aProp = testType.GetProperty("ALocal"); => returns string Type
var sProp = testType.GetProperty("SLocal"); => returns null
Run Code Online (Sandbox Code Playgroud)
我了解内部或公共修饰符之间的差异。