@@ -153,7 +153,7 @@ private MethodBindResult BindMethod(string name, Type[] typeArgs, object[] args,
153153 {
154154 if ( result is MethodBindSuccess )
155155 {
156- result = new MethodBindFailure ( new MissingMemberException ( MiscHelpers . FormatInvariant ( "Object has no method named '{0}' that matches the specified arguments" , name ) ) ) ;
156+ result = new MethodBindFailure ( ( ) => new MissingMemberException ( MiscHelpers . FormatInvariant ( "Object has no method named '{0}' that matches the specified arguments" , name ) ) ) ;
157157 }
158158
159159 foreach ( var altName in GetAltMethodNames ( name ) )
@@ -254,13 +254,13 @@ public static MethodBindResult Create(string name, object rawResult, HostTarget
254254 {
255255 if ( ( method . IsStatic ) & & ! hostTarget . Flags . HasFlag ( HostTargetFlags . AllowStaticMembers ) )
256256 {
257- return new MethodBindFailure ( new InvalidOperationException ( MiscHelpers . FormatInvariant ( "Cannot access static method '{0}' in non-static context" , method . Name ) ) ) ;
257+ return new MethodBindFailure ( ( ) => new InvalidOperationException ( MiscHelpers . FormatInvariant ( "Cannot access static method '{0}' in non-static context" , method . Name ) ) ) ;
258258 }
259259
260260 return new MethodBindSuccess ( hostTarget , method , args ) ;
261261 }
262262
263- return new MethodBindFailure ( ( rawResult as Exception ) ?? new NotSupportedException ( MiscHelpers . FormatInvariant ( "Invocation of method '{0}' failed (unrecognized binding)" , name ) ) ) ;
263+ return new MethodBindFailure ( ( rawResult as Func < Exception > ) ?? ( ( ) => new NotSupportedException ( MiscHelpers . FormatInvariant ( "Invocation of method '{0}' failed (unrecognized binding)" , name ) ) ) ) ;
264264 }
265265
266266 public abstract object RawResult { get ; }
@@ -327,18 +327,18 @@ public override object Invoke(ScriptEngine engine)
327327
328328 private class MethodBindFailure : MethodBindResult
329329 {
330- private readonly Exception exception ;
330+ private readonly Func < Exception > exceptionFactory ;
331331
332- public MethodBindFailure ( Exception exception )
332+ public MethodBindFailure ( Func < Exception > exceptionFactory )
333333 {
334- this . exception = exception ;
334+ this . exceptionFactory = exceptionFactory ;
335335 }
336336
337337 #region MethodBindResult overrides
338338
339339 public override object RawResult
340340 {
341- get { return exception ; }
341+ get { return exceptionFactory ; }
342342 }
343343
344344 public override bool IsPreferredMethod ( string name )
@@ -350,9 +350,10 @@ public override bool IsUnblockedMethod()
350350 {
351351 return false ;
352352 }
353+
353354 public override object Invoke ( ScriptEngine engine )
354355 {
355- throw exception ;
356+ throw exceptionFactory ( ) ;
356357 }
357358
358359 #endregion
@@ -377,7 +378,7 @@ public MethodBindingVisitor(object target, string name, Expression expression)
377378 if ( results . Count != 1 )
378379 {
379380 results . Clear ( ) ;
380- results . Add ( new NotSupportedException ( MiscHelpers . FormatInvariant ( "Invocation of method '{0}' failed (unrecognized binding)" , name ) ) ) ;
381+ AddResult ( ( ) => new NotSupportedException ( MiscHelpers . FormatInvariant ( "Invocation of method '{0}' failed (unrecognized binding)" , name ) ) ) ;
381382 }
382383 else
383384 {
@@ -398,7 +399,7 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
398399 {
399400 if ( node . Method . Name == name )
400401 {
401- results . Add ( node . Method ) ;
402+ AddResult ( node . Method ) ;
402403 }
403404
404405 return base . VisitMethodCall ( node ) ;
@@ -412,7 +413,7 @@ protected override Expression VisitInvocation(InvocationExpression node)
412413 var del = DynamicHelpers . InvokeExpression ( node . Expression ) as Delegate ;
413414 if ( del == targetDelegate )
414415 {
415- results . Add ( del . GetType ( ) . GetMethod ( "Invoke" ) ) ;
416+ AddResult ( del . GetType ( ) . GetMethod ( "Invoke" ) ) ;
416417 }
417418 }
418419
@@ -426,12 +427,22 @@ protected override Expression VisitUnary(UnaryExpression node)
426427 var exception = DynamicHelpers . InvokeExpression ( node . Operand ) as Exception ;
427428 if ( exception != null )
428429 {
429- results . Add ( exception ) ;
430+ AddResult ( ( ) => ( Exception ) DynamicHelpers . InvokeExpression ( node . Operand ) ) ;
430431 }
431432 }
432433
433434 return base . VisitUnary ( node ) ;
434435 }
436+
437+ private void AddResult ( MethodInfo method )
438+ {
439+ results . Add ( method ) ;
440+ }
441+
442+ private void AddResult ( Func < Exception > exceptionFactory )
443+ {
444+ results . Add ( exceptionFactory ) ;
445+ }
435446 }
436447
437448 #endregion
0 commit comments