Click here to Skip to main content
11,415,074 members (62,281 online)
Click here to Skip to main content

Tagged as

Alternative to Activator.CreateInstance

, 25 Jan 2012 CPOL
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,
                          type,
                          new Type[0],
                          target.DeclaringType);
            var il = dynamic.GetILGenerator();
            il.DeclareLocal(target.DeclaringType);
            il.Emit(OpCodes.Newobj, target);
            il.Emit(OpCodes.Stloc_0);
            il.Emit(OpCodes.Ldloc_0);
            il.Emit(OpCodes.Ret);
 
            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).

License

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

Share

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.

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
mvpPaulo 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    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.4 | Last Updated 26 Jan 2012
Article Copyright 2012 by Dean Oliver
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid