示例#1
0
        public bool TryRender(ICSharpSource source, CSharpRenderingContext context)
        {
            if (source is ExecuteMethodDeclaration)
            {
                var directives = context.GetDirectives();
                foreach (var directive in directives)
                {
                    if (!string.Equals(directive.Name, "inject", StringComparison.Ordinal))
                    {
                        continue;
                    }

                    var typeName   = directive.GetValue(RazorDirectiveTokenType.Type);
                    var memberName = directive.GetValue(RazorDirectiveTokenType.Member);

                    context.Writer
                    .WriteLine(_injectAttribute)
                    .Write("public global::")
                    .Write(typeName)
                    .Write(" ")
                    .Write(memberName)
                    .WriteLine(" { get; private set; }");
                }
            }

            return(false);
        }
        private void Render(ExecuteMethodDeclaration source, CSharpRenderingContext context)
        {
            const string DesignTimeHelperMethodName    = "__RazorDesignTimeHelpers__";
            const int    DisableVariableNamingWarnings = 219;

            context.Writer
            .Write("private static object @")
            .Write(DesignTimeVariable)
            .WriteLine(";");

            using (context.Writer.BuildMethodDeclaration("private", "void", "@" + DesignTimeHelperMethodName))
            {
                using (context.Writer.BuildDisableWarningScope(DisableVariableNamingWarnings))
                {
                    context.Writer.WriteVariableDeclaration(typeof(Action).FullName, ActionHelper, value: null);

                    var directives = context.GetDirectives();
                    foreach (var directive in directives)
                    {
                        Render(directive, context);
                    }
                }
            }
        }