Creating objects without Activator.CreateInstance
I am not going to explain why I had to do this.
public static class LambdaActivator
{
public static Func<TResult> Generate<TResult>()
{
var constructorInfo = typeof(TResult).GetConstructor(new Type[] { });
Expression<Func<TResult>> expression = Expression.Lambda<Func<TResult>>(Expression.New(constructorInfo));
Func<TResult> functor = expression.Compile();
return functor;
}
public static Func<object> Generate(Type outputType)
{
var constructorInfo = outputType.GetConstructor(new Type[] { });
Expression<Func<object>> expression = Expression.Lambda<Func<object>>(Expression.New(constructorInfo));
Func<object> functor = expression.Compile();
return functor;
}
}
var result = LambdaActivator.Generate<Something>().Invoke();
var result = LambdaActivator.Generate(typeof(Something)).Invoke() as Something;