예제 #1
0
    private void ValidatePairs(SyntaxNode currentNode, INamedTypeSymbol source, INamedTypeSymbol destination,
                               MappingContext context, Compilation compilation)
    {
        var diagnostics = ImmutableArray.CreateBuilder <Diagnostic>();

        if (!destination.Constructors.Any(_ => _.DeclaredAccessibility == Accessibility.Public ||
                                          destination.ContainingAssembly.ExposesInternalsTo(compilation.Assembly) && _.DeclaredAccessibility == Accessibility.Friend))
        {
            diagnostics.Add(NoAccessibleConstructorsDiagnostic.Create(currentNode));
        }

        var propertyNames = ImmutableArray.CreateBuilder <string>();

        var destinationProperties = destination.GetMembers().OfType <IPropertySymbol>()
                                    .Where(_ => _.SetMethod is not null &&
                                           (_.SetMethod.DeclaredAccessibility == Accessibility.Public ||
                                            (destination.ContainingAssembly.ExposesInternalsTo(compilation.Assembly) && _.SetMethod.DeclaredAccessibility == Accessibility.Friend)))
                                    .ToList();

        foreach (var sourceProperty in source.GetMembers().OfType <IPropertySymbol>()
                 .Where(_ => _.GetMethod is not null &&
                        (_.GetMethod.DeclaredAccessibility == Accessibility.Public ||
                         (source.ContainingAssembly.ExposesInternalsTo(compilation.Assembly) && _.GetMethod.DeclaredAccessibility == Accessibility.Friend))))
        {
            var destinationProperty = destinationProperties.FirstOrDefault(
                _ => _.Name == sourceProperty.Name &&
                context.MatchingPropertyTypeKind switch
            {
                MatchingPropertyTypeKind.Exact => _.Type.Equals(sourceProperty.Type, SymbolEqualityComparer.Default),
                _ => compilation.ClassifyCommonConversion(sourceProperty.Type, _.Type).IsImplicit
            } &&
예제 #2
0
        private static bool IsLegalUsingStatementType(Compilation compilation, ITypeSymbol disposableType, ITypeSymbol type)
        {
            if (disposableType == null)
            {
                return(false);
            }

            // CS1674: type used in a using statement must be implicitly convertible to 'System.IDisposable'
            return(compilation.ClassifyCommonConversion(type, disposableType).IsImplicit);
        }
        private bool IsTypeImplicitlyConvertible(Compilation compilation, ITypeSymbol sourceType, ImmutableArray <ITypeSymbol> targetTypes)
        {
            foreach (var targetType in targetTypes)
            {
                if (compilation.ClassifyCommonConversion(sourceType, targetType).IsImplicit)
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #4
0
        private static bool TypeInfoMatchesType(
            Compilation compilation, TypeInfo argumentTypeInfo, ITypeSymbol parameterType,
            bool isNullLiteral, bool isDefaultLiteral)
        {
            if (parameterType.Equals(argumentTypeInfo.Type) || parameterType.Equals(argumentTypeInfo.ConvertedType))
            {
                return(true);
            }

            if (isDefaultLiteral)
            {
                return(true);
            }

            if (isNullLiteral)
            {
                return(parameterType.IsReferenceType || parameterType.IsNullable());
            }

            // Overload resolution couldn't resolve the actual type of the type parameter. We assume
            // that the type parameter can be the argument's type (ignoring any type parameter constraints).
            if (parameterType.Kind == SymbolKind.TypeParameter)
            {
                return(true);
            }

            // If there's an implicit conversion from the arg type to the param type then
            // count this as a match.  This happens commonly with cases like:
            //
            //  `Goo(derivedType)`
            //  `void Goo(BaseType baseType)`.
            //
            // We want this simple case to match.
            var conversion = compilation.ClassifyCommonConversion(argumentTypeInfo.Type, parameterType);

            if (conversion.IsImplicit)
            {
                return(true);
            }

            return(false);
        }
 public static bool IsAssignableTo(
     [NotNullWhen(returnValue: true)] this ITypeSymbol?fromSymbol,
     [NotNullWhen(returnValue: true)] ITypeSymbol?toSymbol,
     Compilation compilation)
 => fromSymbol != null && toSymbol != null && compilation.ClassifyCommonConversion(fromSymbol, toSymbol).IsImplicit;