示例#1
0
            TCommand WithSetBody(CodeContext code)
            {
                var body = code.GetCode();

                SetStatements = body as BlockSyntax;
                if (body is ExpressionSyntax bodyexpr)
                {
                    SetExpression = SyntaxFactory.ArrowExpressionClause(bodyexpr);
                }
                return((TCommand)this);
            }
示例#2
0
        public override void Transform()
        {
            var implemented = Engine.Select <MethodDeclarationSyntax, ClassDeclarationSyntax>()
                              .Where(x => IsFancyDecoratedMethod(x.SemanticSymbol as IMethodSymbol));

            foreach (var item in implemented)
            {
                var isStatic   = item.SemanticSymbol.IsStatic;
                var methodName = item.Node.Identifier.ToString();
                var decorators = GetDecorators(item.SemanticSymbol as IMethodSymbol);
                var returnType = ConstructReturnType(item.SemanticSymbol as IMethodSymbol);
                var code       = new CodeContext().StartOrContinueWith($"()=>{{({returnType})_{methodName}}}");
                foreach (var decorator in decorators)
                {
                    code = new CodeContext()
                           .InjectType(GetDecoratorFromAttribute(decorator))
                           .As <__DynamicType>()
                           .InjectCode(code.GetCode() as ExpressionSyntax, out var __code)
                           .As(nameof(__code))
                           .InjectCode(GetArguments(decorator), out var __arguments)
                           .As(nameof(__arguments))
                           .StartOrContinueWith(() => new __DynamicType(__arguments).Decorate(__code));
                }

                if (isStatic)
                {
                    item.Parent.Execute((ICreateProperty cmd) =>
                                        cmd.WithName("__decorated" + methodName)
                                        .WithAttributes()
                                        .MakePrivate()
                                        .MakeStatic()
                                        .Returns(returnType)
                                        .InitializeWith(code));

                    var paramNames = item.Node.ParameterList.Parameters.Select(x => x.Identifier.ToString());
                    item.Execute((ICloneMethod cmd) =>
                                 cmd.WithAttributes()
                                 .WithBody(new CodeContext()
                                           .InjectId("__decorated" + methodName, out dynamic __methodName)
                                           .As(nameof(__methodName))
                                           .InjectCode(SyntaxFactory.ParseArgumentList("(" + Join(',', paramNames) + ")"), out var __args)
                                           .As(nameof(__args))
                                           .StartOrContinueWith(() => __methodName(__args))));

                    item.Execute((IModifyMethod cmd) =>
                                 cmd.WithName("_" + methodName)
                                 .MakePrivate()
                                 .MakeStatic()
                                 .WithAttributes());
                }
            }
        }