Most efficient way to get default constructor of a Type

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

    you would want to try FormatterServices.GetUninitializedObject(Type) this one is better than Activator.CreateInstance

    However , this method doesn't call the object constructor , so if you are setting initial values there, this won't work Check MSDN for this thing http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatterservices.getuninitializedobject.aspx

    there is another way here http://www.ozcandegirmenci.com/post/2008/02/Create-object-instances-Faster-than-Reflection.aspx

    however this one fails if the object have parametrize constructors

    Hope this helps

提交回复
热议问题