Exemple #1
0
            public override ITypeSymbol VisitNamedType(INamedTypeSymbol symbol)
            {
                if (symbol.IsAnonymousType)
                {
                    return(symbol);
                }

                // If we don't even have any type arguments, then there's nothing to do.
                var allTypeArguments = symbol.GetAllTypeArguments().ToList();

                if (allTypeArguments.Count == 0)
                {
                    return(symbol);
                }

                // If we have a containing type, make sure its type arguments are updated as well.
                var updatedContainingType = symbol.ContainingType == null
                    ? null
                    : symbol.ContainingType.Accept(this);

                // If our containing type changed, then find us again in the new containing type.
                if (updatedContainingType != symbol.ContainingType)
                {
                    symbol = updatedContainingType.GetTypeMembers(symbol.Name, symbol.Arity).First(m => m.TypeKind == symbol.TypeKind);
                }

                var substitutedArguments = symbol.TypeArguments.Select(t => t.Accept(this)).ToArray();

                if (symbol.TypeArguments.SequenceEqual(substitutedArguments))
                {
                    return(symbol);
                }

                return(typeGenerator.Construct(symbol.OriginalDefinition, substitutedArguments));
            }
Exemple #2
0
            public override ITypeSymbol VisitNamedType(INamedTypeSymbol symbol)
            {
                var mapped = VisitType(symbol);

                if (!Equals(mapped, symbol))
                {
                    return(mapped);
                }

                if (symbol.IsAnonymousType)
                {
                    return(symbol);
                }

                // If we don't even have any type arguments, then there's nothing to do.
                var allTypeArguments = symbol.GetAllTypeArguments().ToList();

                if (allTypeArguments.Count == 0)
                {
                    return(symbol);
                }

                // If we have a containing type, make sure its type arguments are updated as well.
                var updatedContainingType = symbol.ContainingType == null
                    ? null
                    : symbol.ContainingType.Accept(this);

                // If our containing type changed, then find us again in the new containing type.
                if (!Equals(updatedContainingType, symbol.ContainingType))
                {
                    symbol = updatedContainingType.GetTypeMembers(symbol.Name, symbol.Arity).First(m => m.TypeKind == symbol.TypeKind);
                }

                var typeArgumentsWithNullability = symbol.TypeArguments.ZipAsArray(symbol.TypeArgumentNullableAnnotations, (t, n) => t.WithNullability(n));
                var substitutedArguments         = typeArgumentsWithNullability.Select(t => t.Accept(this));

                if (typeArgumentsWithNullability.SequenceEqual(substitutedArguments))
                {
                    return(symbol);
                }

                // TODO: pass nullability to the substituted arguments once https://github.com/dotnet/roslyn/issues/36046 is fixed
                return(_typeGenerator.Construct(symbol.OriginalDefinition, substitutedArguments.Select(t => t.WithoutNullability()).ToArray()).WithNullability(symbol.GetNullability()));
            }