예제 #1
0
        protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            var accessExpression = _error.Reference.GetTreeNode() as IExpression;
            var replacementNode  = accessExpression;

            if (replacementNode == null)
            {
                return(null);
            }

            var modifiers = _declaredElement as IModifiersOwner;

            if (modifiers == null)
            {
                return(null);
            }

            bool isAssign     = replacementNode.Parent is IAssignmentExpression;
            bool needsCasting = !isAssign && !(replacementNode.Parent is IExpressionStatement) &&
                                !_declaredElement.Type().IsVoid() && !_declaredElement.Type().IsObject();

            if (replacementNode.Parent is IInvocationExpression || replacementNode.Parent is IAssignmentExpression)
            {
                replacementNode = (IExpression)replacementNode.Parent;
            }

            CSharpElementFactory factory = CSharpElementFactory.GetInstance(replacementNode, applyCodeFormatter: true);

            AddSystemReflectionNamespace(factory);


            BindingFlags flags = BindingFlags.NonPublic;

            if (modifiers.IsStatic)
            {
                flags |= BindingFlags.Static;
            }
            else
            {
                flags |= BindingFlags.Instance;
            }

            flags |= GetInvokeMemberBindingFlag(_declaredElement, isAssign);

            IExpression instanceExpression = modifiers.IsStatic ? factory.CreateExpression("null") : ((IReferenceExpression)accessExpression).QualifierExpression;
            IExpression argsExpression     = factory.CreateExpression("null");

            if (isAssign)
            {
                argsExpression = factory.CreateExpression("new object[] { $0 }",
                                                          ((IAssignmentExpression)replacementNode).Source);
            }
            if (replacementNode is IInvocationExpression)
            {
                var invocationExpression = (IInvocationExpression)replacementNode;

                if (invocationExpression.Arguments.Count != 0)
                {
                    argsExpression = CreateArrayCreationExpression(
                        TypeFactory.CreateTypeByCLRName(
                            "System.Object",
                            accessExpression.GetPsiModule(),
                            accessExpression.GetResolveContext()), factory);
                    var arrayCreationExpression = argsExpression as IArrayCreationExpression;

                    foreach (var arg in invocationExpression.ArgumentsEnumerable)
                    {
                        var initiallizer = factory.CreateVariableInitializer((ICSharpExpression)arg.Expression);
                        arrayCreationExpression.ArrayInitializer.AddElementInitializerBefore(initiallizer, null);
                    }
                }
            }

            var reflectionExpression = factory.CreateExpression("typeof($0).InvokeMember(\"$1\", $2, null, $3, $4)",
                                                                ((IClrDeclaredElement)_declaredElement).GetContainingType(),
                                                                _declaredElement.ShortName,
                                                                flags.GetFullString(),
                                                                instanceExpression,
                                                                argsExpression);

            if (needsCasting)
            {
                reflectionExpression = factory.CreateExpression("($0)$1",
                                                                _declaredElement.Type(),
                                                                reflectionExpression);
            }

            replacementNode.ReplaceBy(reflectionExpression);
            return(null);
        }