@@ -25,11 +25,13 @@ public static Expression<TDelegate> Parse<TDelegate>(object target, MethodInfo m
25
25
}
26
26
27
27
private readonly struct CacheEntry {
28
- public readonly Expression Body ;
28
+ public readonly Expression Body ;
29
+ public readonly Expression Target ;
29
30
public readonly IList < ParameterExpression > Parameters ;
30
31
31
- public CacheEntry ( Expression body , IList < ParameterExpression > parameters ) {
32
- Body = body ;
32
+ public CacheEntry ( Expression body , Expression target , IList < ParameterExpression > parameters ) {
33
+ Body = body ;
34
+ Target = target ;
33
35
Parameters = parameters ;
34
36
}
35
37
}
@@ -121,6 +123,12 @@ public static LambdaExpression Parse(Type delegateType, Expression targetExpress
121
123
122
124
{
123
125
var map = new Dictionary < Expression , Expression > ( ) ;
126
+ var target = targetExpression ;
127
+ if ( target != null ) {
128
+ target = Expression . Constant ( targetExpression . Type . IsValueType ? Activator . CreateInstance ( targetExpression . Type ) : null ,
129
+ targetExpression . Type ) ;
130
+ map . Add ( targetExpression , target ) ;
131
+ }
124
132
125
133
if ( arguments != null ) {
126
134
for ( var i = 0 ; i < arguments . Count ; i ++ ) {
@@ -133,7 +141,7 @@ public static LambdaExpression Parse(Type delegateType, Expression targetExpress
133
141
lock ( CACHE ) {
134
142
if ( ! CACHE . TryGetValue ( cacheKey , out cached ) ) {
135
143
136
- cached = new CacheEntry ( body , parameters ) ;
144
+ cached = new CacheEntry ( body , target , parameters ) ;
137
145
CACHE . Add ( cacheKey , cached ) ;
138
146
}
139
147
}
@@ -143,8 +151,10 @@ public static LambdaExpression Parse(Type delegateType, Expression targetExpress
143
151
144
152
Expression bind ( CacheEntry entry ) {
145
153
Expression cachedBody = entry . Body ;
146
- if ( parameters . Any ( ) ) {
154
+ if ( entry . Target != null || parameters . Any ( ) ) {
147
155
var map = new Dictionary < Expression , Expression > ( ) ;
156
+ if ( entry . Target != null )
157
+ map . Add ( entry . Target , targetExpression ) ;
148
158
149
159
for ( var i = 0 ; i < entry . Parameters . Count ; i ++ ) {
150
160
var arg = arguments ? [ i ] ;
0 commit comments