The most efficient way to get a standard type constructor

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

I thought something like the lines below, but it seems like there should be a simpler and more efficient way to do this.

Type type = typeof(FooBar) BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; type.GetConstructors(flags) .Where(constructor => constructor.GetParameters().Length == 0) .First(); 
+56
reflection c #
Sep 26 '08 at 22:28
source share
5 answers
 type.GetConstructor(Type.EmptyTypes) 
+109
Sep 26 '08 at 22:30
source share

If you really need a ConstructorInfo object, see Kurt Hagenloher's answer .

On the other hand, if you are really just trying to create an object at runtime with System.Type , see System.Activator.CreateInstance - this is not only a reliable future (the activator handles more details than ConstructorInfo.Invoke ), it is also much less ugly.

+26
Sep 26 '08 at 23:08
source share

If you have a generic type type, then Jeff Bridgman's answer is the best. If you only have a type object representing the type you want to build, you can use Activator.CreateInstance(Type) as suggested by Alex Lyman, but they told me that it is slow (I did not profile it personally, though).

However, if you build these objects very often, there is a more eloquent approach using dynamically compiled Linq expressions:

 using System; using System.Linq.Expressions; public static class TypeHelper { public static Func<object> CreateDefaultConstructor(Type type) { NewExpression newExp = Expression.New(type); // Create a new lambda expression with the NewExpression as the body. var lambda = Expression.Lambda<Func<object>>(newExp); // Compile our new lambda expression. return lambda.Compile(); } } 

Just call the delegate who was returned to you. You must cache this delegate because constantly recompiling Linq expressions can be expensive, but if you cache the delegate and reuse it every time, it can be very fast! I personally use a static search dictionary indexed by type. This function is useful when you are dealing with serialized objects where you can only know type information.

NOTE. This may fail if the type is not constructive or does not have a default constructor!

+1
Jul 09 '13 at 16:04 on
source share

If you want the default constructor to instantiate the class and get the type as a typical type parameter for the function, you can do the following:

 T NewItUp<T>() where T : new() { return new T(); } 
0
Apr 16 '13 at
source share

would you like to try FormatterServices.GetUninitializedObject (Type) is better than Activator.CreateInstance

However, this method does not call the constructor of the object, so if you set the initial values โ€‹โ€‹there, it will not work. Check the 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 fails if the object has parameterization constructors

Hope this helps

-2
Sep 27 '08 at 0:33
source share



All Articles