Click here to Skip to main content
13,147,383 members (31,619 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 26 Jan 2012

Alternative to Activator.CreateInstance

, 26 Jan 2012
Rate this:
Please Sign up or sign in to vote.
The final solution after much debate and optimization:public class DynamicInitializer { static readonly Dictionary> list = new Dictionary>(); public static T New() where T : class { return New(typeof...

The final solution after much debate and optimization:

public class DynamicInitializer
        static readonly Dictionary<string, Func<object>> list = new Dictionary<string, Func<object>>();
        public static T New<T>() where T : class 
            return New(typeof (T)) as T;
        public static object New(Type type)
            if (list.ContainsKey(type.Name)) return list[type.Name];
            Func<object> method = Expression.Lambda<Func<object>>(Expression.Block(type, new Expression[] { Expression.New(type) })).Compile();
            list.Add(type.Name, method);
            return method();


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

GeneralRe: Perhaps it would make it completely obvious looking at the c... Pin
Andrew Rissing26-Jan-12 6:46
memberAndrew Rissing26-Jan-12 6:46 
GeneralRe: If you use TryGetValue, it uses an out parameter to set the ... Pin
Andrew Rissing26-Jan-12 6:33
memberAndrew Rissing26-Jan-12 6:33 
GeneralRe: thanks Andrew appreciate that. Why is it quicker though? Pin
Dean Oliver26-Jan-12 5:50
memberDean Oliver26-Jan-12 5:50 
GeneralRe: If you do update this, I'd recommend using "TryGetValue" rat... Pin
Andrew Rissing26-Jan-12 5:13
memberAndrew Rissing26-Jan-12 5:13 
GeneralRe: No problem. Pin
Paulo Zemek26-Jan-12 5:02
mvpPaulo Zemek26-Jan-12 5:02 
GeneralI think this last version is almost OK. In fact, if you know... Pin
Paulo Zemek26-Jan-12 3:59
mvpPaulo Zemek26-Jan-12 3:59 
GeneralRe: Sorry Paulo I actually accidentally posted the incorrect ver... Pin
Dean Oliver26-Jan-12 4:42
memberDean Oliver26-Jan-12 4:42 
GeneralBased on a comment by nonexisto, I realized that the constra... Pin
Andrew Rissing26-Jan-12 3:49
memberAndrew Rissing26-Jan-12 3:49 

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 | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 26 Jan 2012
Article Copyright 2012 by Dean Oliver
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid