-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathFastActivator.cs
More file actions
32 lines (30 loc) · 1.04 KB
/
Copy pathFastActivator.cs
File metadata and controls
32 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Linq.Expressions;
namespace PaySharp.Core
{
public static class FastActivator
{
public static Func<T, TResult> Generate<T, TResult>()
{
ConstructorInfo constructorInfo = typeof(TResult).GetConstructor(new Type[] { typeof(T), });
#if DEBUG
ParameterInfo[] parameters = constructorInfo.GetParameters();
ParameterExpression parameterExpression = Expression.Parameter(typeof(T), parameters[0].Name);
#else
ParameterExpression parameterExpression = Expression.Parameter(typeof(T));
#endif
Expression<Func<T, TResult>> expression = Expression.Lambda<Func<T, TResult>>(
Expression.New(
constructorInfo,
parameterExpression),
parameterExpression);
Func<T, TResult> functor = expression.Compile();
return functor;
}
}
}