Ejemplo n.º 1
0
        public void Execute(GeneratorExecutionContext context)
        {
            if (context.Compilation is not CSharpCompilation)
            {
                return;
            }

            var attrCode = new IDisposableGeneratorAttributeTemplate().TransformText();

            context.AddSource(AttributeName + ".cs", attrCode);

            try
            {
                if (context.SyntaxReceiver is not SyntaxReceiver receiver)
                {
                    return;
                }

                foreach (var(classDeclaration, attributeSyntax) in receiver.Targets)
                {
                    var model      = context.Compilation.GetSemanticModel(classDeclaration.SyntaxTree);
                    var typeSymbol = model.GetDeclaredSymbol(classDeclaration);
                    if (typeSymbol is null)
                    {
                        continue;
                    }

                    var genArgs  = new GeneratorArgument(model, attributeSyntax);
                    var template = new CodeTemplate(classDeclaration, genArgs)
                    {
                        Namespace = typeSymbol.ContainingNamespace.ToDisplayString(),
                    };

                    if (context.CancellationToken.IsCancellationRequested)
                    {
                        return;
                    }

                    var text = template.TransformText();
                    context.AddSource(typeSymbol.GenerateHintName(), text);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
        }
Ejemplo n.º 2
0
        internal CodeTemplate(ClassDeclarationSyntax classDeclaration, GeneratorArgument genArg)
        {
            ClassName = classDeclaration.GetGenericTypeName();

            var compositeDisposableTypeSymbolName = genArg.CompositeDisposableTypeSymbol?.ToString();

            if (!string.IsNullOrWhiteSpace(compositeDisposableTypeSymbolName))
            {
                CompositeDisposableTypeName   = compositeDisposableTypeSymbolName !;
                UseDefaultCompositeDisposable = false;
            }
            else
            {
                CompositeDisposableTypeName   = DefaultTypeName;
                UseDefaultCompositeDisposable = true;
            }

            CompositeDisposableFieldName = GetFieldName(genArg.CompositeDisposableFieldName);
            Options = genArg.Options;