Click here to Skip to main content
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

 
QuestionActivator.CreateInstance() is faster than this Pinmembersobo1231-Jan-14 11:43 
GeneralRe: Ignore that, nm...that would only apply to a public construc... PinmemberAndrew Rissing26-Jan-12 4:37 
GeneralRe: Good reply, I agree in this case I'd use a "static" function... Pinmemberjohannesnestler26-Jan-12 12:52 
GeneralRe: Btw, you might want to add a default constructor constraint ... PinmemberAndrew Rissing25-Jan-12 16:49 
GeneralRe: I would disagree with statics being considered a bad practic... PinmemberAndrew Rissing25-Jan-12 10:19 
GeneralI don't see any problem with statics. If it is for performan... PinmvpPaulo Zemek25-Jan-12 10:55 
GeneralBtw, you can mark these as all static. PinmemberAndrew Rissing25-Jan-12 8:40 
GeneralRe: Its actually considered to be bad practice to have static me... PinmemberDean 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
Web02 | 2.8.141223.1 | Last Updated 26 Jan 2012
Article Copyright 2012 by Dean Oliver
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid