コード例 #1
0
        private static List <INamedTypeSymbol> GetClassSymbols(GeneratorExecutionContext context, SyntaxReceiver receiver)
        {
            var compilation  = context.Compilation;
            var classSymbols = new List <INamedTypeSymbol>();

            foreach (var clazz in receiver.CandidateClasses)
            {
                var model       = compilation.GetSemanticModel(clazz.SyntaxTree);
                var classSymbol = model.GetDeclaredSymbol(clazz) !;
                if (classSymbol !.GetAttributes().Any(ad => ad.AttributeClass !.Name == "PrimaryConstructorAttribute"))
                {
                    classSymbols.Add(classSymbol);
                }
            }

            return(classSymbols);
        }
コード例 #2
0
        private static List <INamedTypeSymbol> GetClassSymbols(GeneratorExecutionContext context, SyntaxReceiver receiver)
        {
            var options     = (context.Compilation as CSharpCompilation).SyntaxTrees[0].Options as CSharpParseOptions;
            var compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(primaryConstructorAttributeText, Encoding.UTF8), options));

            var attributeSymbol = compilation.GetTypeByMetadataName("PrimaryConstructorAttribute") !;

            var classSymbols = new List <INamedTypeSymbol>();

            foreach (var clazz in receiver.CandidateClasses)
            {
                var model       = compilation.GetSemanticModel(clazz.SyntaxTree);
                var classSymbol = model.GetDeclaredSymbol(clazz) !;
                if (classSymbol.GetAttributes().Any(ad => ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default)))
                {
                    classSymbols.Add(classSymbol);
                }
            }

            return(classSymbols);
        }