-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2.新增FastActivator
- Loading branch information
dongfo
committed
Mar 4, 2019
1 parent
1e0c28e
commit 280496d
Showing
2 changed files
with
36 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters