예제 #1
0
        TranslatedExpression HandleDelegateConstruction(CallInstruction inst)
        {
            ILInstruction func = inst.Arguments[1];
            IMethod       method;

            switch (func.OpCode)
            {
            case OpCode.LdFtn:
                method = ((LdFtn)func).Method;
                break;

            case OpCode.LdVirtFtn:
                method = ((LdVirtFtn)func).Method;
                break;

            default:
                method = (IMethod)typeSystem.Resolve(((ILFunction)func).Method);
                break;
            }
            var target = expressionBuilder.TranslateTarget(method, inst.Arguments[0], func.OpCode == OpCode.LdFtn);
            var lookup = new MemberLookup(resolver.CurrentTypeDefinition, resolver.CurrentTypeDefinition.ParentAssembly);
            var or     = new OverloadResolution(resolver.Compilation, method.Parameters.SelectArray(p => new TypeResolveResult(p.Type)));
            var result = lookup.Lookup(target.ResolveResult, method.Name, method.TypeArguments, true) as MethodGroupResolveResult;

            if (result == null)
            {
                target = target.ConvertTo(method.DeclaringType, expressionBuilder);
            }
            else
            {
                or.AddMethodLists(result.MethodsGroupedByDeclaringType.ToArray());
                if (or.BestCandidateErrors != OverloadResolutionErrors.None || !IsAppropriateCallTarget(method, or.BestCandidate, func.OpCode == OpCode.LdVirtFtn))
                {
                    target = target.ConvertTo(method.DeclaringType, expressionBuilder);
                }
            }

            var mre = new MemberReferenceExpression(target, method.Name);

            mre.TypeArguments.AddRange(method.TypeArguments.Select(expressionBuilder.ConvertType));
            var oce = new ObjectCreateExpression(expressionBuilder.ConvertType(inst.Method.DeclaringType), mre)
                      //				.WithAnnotation(new DelegateConstruction.Annotation(func.OpCode == OpCode.LdVirtFtn, target, method.Name))
                      .WithILInstruction(inst)
                      .WithRR(new ConversionResolveResult(
                                  inst.Method.DeclaringType,
                                  new MemberResolveResult(target.ResolveResult, method),
                                  // TODO handle extension methods capturing the first argument
                                  Conversion.MethodGroupConversion(method, func.OpCode == OpCode.LdVirtFtn, false)));

            if (func is ILFunction)
            {
                return(expressionBuilder.TranslateFunction(oce, target, (ILFunction)func));
            }
            else
            {
                return(oce);
            }
        }
예제 #2
0
 public IType Resolve(Mono.Cecil.TypeReference typeReference, bool isFromSignature)
 {
     return(context.Resolve(typeReference, isFromSignature).AcceptVisitor(substitution));
 }
 public IType Resolve(Mono.Cecil.TypeReference typeReference)
 {
     return(context.Resolve(typeReference).AcceptVisitor(substitution));
 }