简体   繁体   English

受保护的构造函数-不与GetConstructors()一起显示

[英]Protected constructor - not showing with GetConstructors()

I have a class with a factory method and a protected constructor defined to take a few arguments. 我有一个带有工厂方法的类和一个受保护的构造函数,定义了一些参数。

public class MyClass
{
    protected void MyClass(int agr1, int arg2)
    {
      //set private backing fields for public readonly properties
    }

    public static MyClass From(int arg1, int arg2)
    {
          return new MyClass(arg1, arg2); 
    }
} 

Yet when I use typeof(MyClass).GetConstructors(BindingFlags.NonPublic) the array returned has zero items in it. 但是,当我使用typeof(MyClass).GetConstructors(BindingFlags.NonPublic) ,返回的数组中包含零项。 Anyone see what I'm doing wrong? 有人看到我在做什么错吗?

Thanks 谢谢

Since you are passing the BindingFlags yourself, you must not forget to include BindingFlags.Instance if necessary (which in this case, it is). 由于您自己传递了BindingFlags ,因此如果有必要(在这种情况下,就是这样),您一定不要忘记包含BindingFlags.Instance

Including it will work: 包括它将起作用:

var flags = BindingFlags.NonPublic | BindingFlags.Instance
var ctors = typeof(MyClass).GetConstructors(flags);

See it in action . 看到它在行动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM