How to call protected constructor in c#?

后端 未结 8 1912
不知归路
不知归路 2021-02-19 00:51

How to call protected constructor?

public class Foo{
  public Foo(a lot of arguments){}
  protected Foo(){}
}
var foo=???

This obviously fails

8条回答
  •  自闭症患者
    2021-02-19 01:22

    Serj-Tm answered adequately but Activator can do it too:

    var foo = (Foo) Activator.CreateInstance(typeof(Foo), 
                   BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, 
                   null, 
                   new object[] { 2.0 }, 
                   CultureInfo.InvariantCulture);
    

提交回复
热议问题