internal Delegate CreateDelegate(StrongBox<object>[] closure) { if (_compiled != null) { return CreateCompiledDelegate(closure); } // Otherwise, we'll create an interpreted LightLambda var ret = new LightLambda(_interpreter, closure, this); lock (this) { // If this field is now null, it means a compile happened if (_lightLambdas != null) { _lightLambdas.Add(ret); } } if (_lightLambdas == null) { return CreateCompiledDelegate(closure); } return ret.MakeDelegate(_lambda.Type); }
private Delegate CreateCompiledDelegate(StrongBox<object>[] closure) { // It's already compiled, and the types match, just use the // delegate directly. Delegate d = _compiled(closure); // The types might not match, if the delegate type we want is // not a Func/Action. In that case, use LightLambda to create // a new delegate of the right type. This is not the most // efficient approach, but to do better we need the ability to // compile into a DynamicMethod that we created. if (d.GetType() != _lambda.Type) { var ret = new LightLambda(_interpreter, closure, this); ret.Compiled = d; d = ret.MakeDelegate(_lambda.Type); } return d; }