Most efficient way to get default constructor of a Type

后端 未结 5 1080
礼貌的吻别
礼貌的吻别 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:39

    If you actually need the ConstructorInfo object, then see Curt Hagenlocher's answer.

    On the other hand, if you're really just trying to create an object at run-time from a System.Type, see System.Activator.CreateInstance -- it's not just future-proofed (Activator handles more details than ConstructorInfo.Invoke), it's also much less ugly.

提交回复
热议问题