CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Using Lambda instead Activator.CreateInstance

405 Views
Copy Code Show/Hide Line Numbers
// usage:
// var constructor = GetConstructor<Foo>();
// IFoo foo = constructor();
 
 
private static Func<T> GetConstructor<T>() where T : class, new()
{
    ConstructorInfo constructorInfo = typeof(T).GetConstructor(new Type[0]);
    return Expression.Lambda<Func<T>>(Expression.New(constructorInfo))
                .Compile();
}
 
by tarasn
  March 26, 2010 @ 12:21pm
Tags:
Description:
This code shows how to use Lambda to inistiate new types
copied from : http://www.smelser.net/blog/post/2010/03/05/When-Activator-is-just-to-slow.aspx

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate