Most efficient way to get default constructor of a Type

后端 未结 5 1021
礼貌的吻别
礼貌的吻别 2021-02-02 05:00

What is the most efficient way to get the default constructor (i.e. instance constructor with no parameters) of a System.Type?

I was thinking something along the lines o

5条回答
  •  时光取名叫无心
    2021-02-02 05:40

    If you only want to get the default constructor to instantiate the class, and are getting the type as a generic type parameter to a function, you can do the following:

    T NewItUp() where T : new()
    {
       return new T();
    }
    

提交回复
热议问题