Click here to Skip to main content
13,861,490 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


15 bookmarked
Posted 25 Jan 2012
Licenced CPOL

Alternative to Activator.CreateInstance

, 25 Jan 2012
Rate this:
Please Sign up or sign in to vote.
An alternative to Activator.CreateInstance

I found that the quickest way to instantiate an object with a default constructor through Reflection is by using the DynamicMethod class. Below is a helper class I wrote to easily instantiate an object using Reflection.

public class DynamicInitializer
      public static V NewInstance<V>() where V : class
          return ObjectGenerator(typeof(V)) as V;

      private static object ObjectGenerator(Type type)
          var target = type.GetConstructor(Type.EmptyTypes);
          var dynamic = new DynamicMethod(string.Empty,
                        new Type[0],
          var il = dynamic.GetILGenerator();
          il.Emit(OpCodes.Newobj, target);

          var method = (Func<object>)dynamic.CreateDelegate(typeof(Func<object>));
          return method();

      public static object NewInstance(Type type)
          return ObjectGenerator(type);

You can call NewInstance using two methods: NewInstance<V>(), NewInstance(Type type).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dean Oliver
Software Developer BBD Johannesburg
South Africa South Africa
Bsc (Hons) Business Information Systems.
MCTS: Web Applications Development with Microsoft .NET Framework 4
MCTS: Windows Communication Foundation Development with Microsoft .NET Framework 4
MCTS: Accessing Data with Microsoft .NET Framework 4
Microsoft Certified Professional Developer Certification.

You may also be interested in...

Comments and Discussions

QuestionWorth noting... Pin
Cryptonite13-Mar-15 14:35
memberCryptonite13-Mar-15 14:35 
QuestionActivator.CreateInstance() is faster than this Pin
sobo1231-Jan-14 11:43
membersobo1231-Jan-14 11:43 
GeneralRe: Ignore that, nm...that would only apply to a public construc... Pin
Andrew Rissing26-Jan-12 4:37
memberAndrew Rissing26-Jan-12 4:37 
GeneralRe: Good reply, I agree in this case I'd use a "static" function... Pin
johannesnestler26-Jan-12 12:52
memberjohannesnestler26-Jan-12 12:52 
GeneralRe: Btw, you might want to add a default constructor constraint ... Pin
Andrew Rissing25-Jan-12 16:49
memberAndrew Rissing25-Jan-12 16:49 
GeneralRe: I would disagree with statics being considered a bad practic... Pin
Andrew Rissing25-Jan-12 10:19
memberAndrew Rissing25-Jan-12 10:19 
GeneralI don't see any problem with statics. If it is for performan... Pin
Paulo Zemek25-Jan-12 10:55
professionalPaulo Zemek25-Jan-12 10:55 
GeneralBtw, you can mark these as all static. Pin
Andrew Rissing25-Jan-12 8:40
memberAndrew Rissing25-Jan-12 8:40 
GeneralRe: Its actually considered to be bad practice to have static me... Pin
Dean Oliver25-Jan-12 9:40
memberDean Oliver25-Jan-12 9:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 26 Jan 2012
Article Copyright 2012 by Dean Oliver
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid