private Expression HandleAnonymousType()
        {
            Instruction     instruction;
            MethodReference methodReference = null;
            ConstructorInfo constructorInfo = null;

            RootScope.TryGetInstruction(_objectCreation, OpCodes.Newobj, out instruction);
            methodReference = instruction.Operand as MethodReference;
            constructorInfo = methodReference.GetActualMethod <ConstructorInfo>();

            _arguments = _objectCreation.Initializer
                         .Elements
                         .Select(e => e.AcceptVisitor(Visitor, ParentScope))
                         .ToListOf <Expression>();

            InternalType = constructorInfo.DeclaringType;

            return(_arguments != null?Expression.New(constructorInfo, _arguments)
                       : Expression.New(constructorInfo));
        }
        private bool TryHandleAnonymousMethod(NRefactory.Expression @this, NRefactory.InvocationExpression func, out Expression outExpression)
        {
            MethodReference methodReference = null;

            NRefactory.IdentifierExpression methodIdentifier = null;

            outExpression = null;

            if (func != null)
            {
                methodIdentifier = func.Arguments.Single() as NRefactory.IdentifierExpression;
                methodReference  = methodIdentifier.Annotation <MethodReference>();

                if (methodReference != null)
                {
                    object   target     = null;
                    Delegate @delegate  = null;
                    var      methodInfo = methodReference.GetActualMethod() as MethodInfo;

                    if (!methodInfo.IsStatic)
                    {
                        var context = RootScope.Context;

                        if (context != null)
                        {
                            target = context.Value;
                        }
                    }

                    @delegate     = methodInfo.CreateDelegate(InternalType, target);
                    outExpression = Expression.Constant(@delegate);
                    return(true);
                }
            }

            return(false);
        }
Example #3
0
        public override AstNode Emit()
        {
            MethodBase      method          = null;
            bool            isDelegate      = false;
            MethodReference methodReference = null;

            if (Node.Target.ToString().Equals("ldftn", StringComparison.OrdinalIgnoreCase))
            {
                methodReference = Node.Arguments.First().Annotation <MethodReference>();
                method          = methodReference.GetActualMethod <MethodInfo>();
                ILGenerator.Emit(OpCodes.Ldftn, method as MethodInfo);
            }
            else
            {
                methodReference = Node.Annotation <MethodReference>();
                isDelegate      = IsDelegate(methodReference);
                method          = methodReference.GetActualMethod();

                if (method.DeclaringType.Namespace.Equals("System.Reflection"))
                {
                    throw new NotSupportedException("Emitting constructors through System.Reflection is not supported");
                }

                if (isDelegate)
                {
                    Node.Target.AcceptVisitor(Visitor, ILGenerator);
                }

                if (method.IsPrivate || isDelegate)
                {
                    var methodInfo = method as MethodInfo;

                    Type = methodInfo.ReturnType;

                    if (isDelegate)
                    {
                        EmitPrivateDelegate(methodInfo);
                    }
                    else
                    {
                        EmitPrivate(methodInfo);
                    }
                }
                else
                {
                    var parameters = method.GetParameters();

                    Node.Arguments.ForEach((arg, i) => {
                        var parameterType = parameters[i].ParameterType;
                        var expression    = arg.AcceptVisitor(ILGenerator, Visitor);

                        ILGenerator.EmitCastIfNeeded(expression.Type, parameterType);
                    });

                    if (method.IsConstructor)
                    {
                        var ctor = method as ConstructorInfo;

                        Type = ctor.DeclaringType;
                        ILGenerator.Emit(OpCodes.Call, ctor);
                    }
                    else
                    {
                        var methodInfo = method as MethodInfo;

                        Type = methodInfo.ReturnType;
                        ILGenerator.Emit(OpCodes.Call, methodInfo);
                    }
                }
            }

            return(base.Emit());
        }