Click here to Skip to main content
Licence CPOL
First Posted 26 Jan 2012
Views 550
Bookmarked 1 time

Alternative to Activator.CreateInstance

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();
        }
    }

License

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 (Junior)
Comcorp Holdings
South Africa South Africa

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRe: Perhaps it would make it completely obvious looking at the c... PinmemberAndrew Rissing6:46 26 Jan '12  
GeneralRe: If you use TryGetValue, it uses an out parameter to set the ... PinmemberAndrew Rissing6:33 26 Jan '12  
GeneralRe: thanks Andrew appreciate that. Why is it quicker though? PinmemberDean Oliver5:50 26 Jan '12  
GeneralRe: If you do update this, I'd recommend using "TryGetValue" rat... PinmemberAndrew Rissing5:13 26 Jan '12  
GeneralRe: No problem. PinmvpPaulo Zemek5:02 26 Jan '12  
GeneralI think this last version is almost OK. In fact, if you know... PinmvpPaulo Zemek3:59 26 Jan '12  
GeneralRe: Sorry Paulo I actually accidentally posted the incorrect ver... PinmemberDean Oliver4:42 26 Jan '12  
GeneralBased on a comment by nonexisto, I realized that the constra... PinmemberAndrew Rissing3:49 26 Jan '12  

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.

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