using System; using System.Collections.Generic; using System.Linq.Expressions; namespace VAR.Focus.Web.Code { public class ObjectActivator { private static Dictionary> _creators = new Dictionary>(); public static Func GetLambdaNew(Type type) { if (_creators.ContainsKey(type)) { return _creators[type]; } lock (_creators) { NewExpression newExp = Expression.New(type); LambdaExpression lambda = Expression.Lambda(typeof(Func), newExp); Func compiledLambdaNew = (Func)lambda.Compile(); _creators.Add(type, compiledLambdaNew); } return _creators[type]; } public static object CreateInstance(Type type) { Func creator = GetLambdaNew(type); return creator(); } } }