예제 #1
0
        public void Execute(GeneratorExecutionContext context)
        {
            var rx = (SyntaxReceiver)context.SyntaxContextReceiver !;

#pragma warning disable RS1024 // Compare symbols correctly (https://github.com/dotnet/roslyn-analyzers/issues/4469)
            var trackSeenTypes = new HashSet <INamedTypeSymbol>(SymbolEqualityComparer.Default);
#pragma warning restore RS1024 // Compare symbols correctly

            // Go through each entry and add the appropriate types.
            foreach (var(node, options) in rx.Types)
            {
                var semanticModel = context.Compilation.GetSemanticModel(node.SyntaxTree);

                var structSymbol = semanticModel.GetDeclaredSymbol(node);

                if (!trackSeenTypes.Add(structSymbol))
                {
                    // Seen already - duplicate attribute usage warning is going to show up.
                    continue;
                }

                // First, validate.
                if (ValidateType(context, node, structSymbol, options))
                {
                    // Use the backing type to pick the generator.
                    ITypeGenerator generator = options.BackingType switch
                    {
                        IdBackingType.Guid => GuidGenerator,
                        IdBackingType.Int => IntGenerator,
                        IdBackingType.Long => LongGenerator,
                        IdBackingType.String => StringGenerator,
                        _ => null,
                    };

                    if (generator is ITypeGenerator)
                    {
                        generator.CreateTypeExtension(context, structSymbol, options);
                    }
                }
            }
        }