相关疑难解决方法(0)

如何查找C#类的内部属性?保护?保护内部?

如果我有一个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上的其他辅助功能类型似乎没有标志.

如何获取仅包含内部属性的列表/数组?在相关的说明中,但对我的应用程序来说不是必需的,那么受保护或受保护的内部呢?

c# system.reflection

10
推荐指数
2
解决办法
4558
查看次数

GetProperty 方法返回 null C# .Net

有人可以解释一下,为什么 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)

我了解内部或公共修饰符之间的差异。

.net c# access-modifiers

2
推荐指数
1
解决办法
3507
查看次数

标签 统计

c# ×2

.net ×1

access-modifiers ×1

system.reflection ×1