当前位置: 首页>>代码示例>>C#>>正文


C# TypeSymbol.GetEffectiveNullability方法代码示例

本文整理汇总了C#中TypeSymbol.GetEffectiveNullability方法的典型用法代码示例。如果您正苦于以下问题:C# TypeSymbol.GetEffectiveNullability方法的具体用法?C# TypeSymbol.GetEffectiveNullability怎么用?C# TypeSymbol.GetEffectiveNullability使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TypeSymbol的用法示例。


在下文中一共展示了TypeSymbol.GetEffectiveNullability方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ClassifyNullLiteralConversion

        private static ConversionKind ClassifyNullLiteralConversion(BoundExpression source, TypeSymbol destination)
        {
            Debug.Assert((object)source != null);
            Debug.Assert((object)destination != null);

            if (!source.IsLiteralNull())
            {
                return ConversionKind.NoConversion;
            }

            // SPEC: An implicit conversion exists from the null literal to any nullable type. 
            if (destination.IsNullableType())
            {
                // The spec defines a "null literal conversion" specifically as a conversion from
                // null to nullable type.
                return ConversionKind.NullLiteral;
            }

            // SPEC: An implicit conversion exists from the null literal to any reference type. 
            // SPEC: An implicit conversion exists from the null literal to type parameter T, 
            // SPEC: provided T is known to be a reference type. [...] The conversion [is] classified 
            // SPEC: as implicit reference conversion. 

            if (destination.IsReferenceType)
            {
                if (destination.GetEffectiveNullability() != EffectiveNullability.Nullable)
                    return ConversionKind.NoConversion;
                return ConversionKind.ImplicitReference;
            }

            // SPEC: The set of implicit conversions is extended to include...
            // SPEC: ... from the null literal to any pointer type.

            if (destination is PointerTypeSymbol)
            {
                return ConversionKind.NullToPointer;
            }

            return ConversionKind.NoConversion;
        }
开发者ID:jeffanders,项目名称:roslyn,代码行数:40,代码来源:Conversions.cs

示例2: GenerateImplicitConversionError

        protected void GenerateImplicitConversionError(DiagnosticBag diagnostics, CSharpSyntaxNode syntax,
            Conversion conversion, BoundExpression expression, TypeSymbol targetType)
        {
            Debug.Assert(expression != null);
            Debug.Assert((object)targetType != null);

            if (targetType.TypeKind == TypeKind.Error)
            {
                return;
            }

            if (expression.Kind == BoundKind.BadExpression)
            {
                return;
            }

            if (expression.Kind == BoundKind.UnboundLambda)
            {
                GenerateAnonymousFunctionConversionError(diagnostics, syntax, (UnboundLambda)expression, targetType);
                return;
            }

            var sourceType = expression.Type;
            if ((object)sourceType != null)
            {
                GenerateImplicitConversionError(diagnostics, this.Compilation, syntax, conversion, sourceType, targetType, expression.ConstantValue);
                return;
            }

            if (expression.IsLiteralNull())
            {
                if (targetType.GetEffectiveNullability() != EffectiveNullability.Nullable)
                {
                    Error(diagnostics, ErrorCode.ERR_NullAssignedToNonNullableType, syntax, targetType);
                    return;
                }
                if (targetType.TypeKind == TypeKind.TypeParameter)
                {
                    Error(diagnostics, ErrorCode.ERR_TypeVarCantBeNull, syntax, targetType);
                    return;
                }
                if (targetType.IsValueType)
                {
                    Error(diagnostics, ErrorCode.ERR_ValueCantBeNull, syntax, targetType);
                    return;
                }
            }

            if (expression.Kind == BoundKind.MethodGroup)
            {
                var methodGroup = (BoundMethodGroup)expression;
                if (!Conversions.ReportDelegateMethodGroupDiagnostics(this, methodGroup, targetType, diagnostics))
                {
                    var nodeForSquiggle = syntax;
                    while (nodeForSquiggle.Kind() == SyntaxKind.ParenthesizedExpression)
                    {
                        nodeForSquiggle = ((ParenthesizedExpressionSyntax)nodeForSquiggle).Expression;
                    }

                    if (nodeForSquiggle.Kind() == SyntaxKind.SimpleMemberAccessExpression || nodeForSquiggle.Kind() == SyntaxKind.PointerMemberAccessExpression)
                    {
                        nodeForSquiggle = ((MemberAccessExpressionSyntax)nodeForSquiggle).Name;
                    }

                    var location = nodeForSquiggle.Location;

                    if (ReportDelegateInvokeUseSiteDiagnostic(diagnostics, targetType, location))
                    {
                        return;
                    }

                    Error(diagnostics,
                        targetType.IsDelegateType() ? ErrorCode.ERR_MethDelegateMismatch : ErrorCode.ERR_MethGrpToNonDel,
                        location, methodGroup.Name, targetType);
                }

                return;
            }

            Debug.Assert(expression.HasAnyErrors && expression.Kind != BoundKind.UnboundLambda, "Missing a case in implicit conversion error reporting");
        }
开发者ID:jeffanders,项目名称:roslyn,代码行数:81,代码来源:Binder_Statements.cs

示例3: ClassifyImplicitConversionFromExpression

        /// <summary>
        /// Determines if the source expression is convertible to the destination type via
        /// any built-in or user-defined implicit conversion.
        /// </summary>
        private Conversion ClassifyImplicitConversionFromExpression(BoundExpression sourceExpression, TypeSymbol source, TypeSymbol destination, ref HashSet<DiagnosticInfo> useSiteDiagnostics)
        {
            Debug.Assert(sourceExpression != null || (object)source != null);
            Debug.Assert(sourceExpression == null || (object)sourceExpression.Type == (object)source);
            Debug.Assert((object)destination != null);

            //PERF: identity conversion is by far the most common implicit conversion, check for that first
            if ((object)source != null && HasIdentityConversion(source, destination))
            {
                // Will this too negatively impact performance? If so, should consider actually making T!? a separate TypeSymbol and default(T!?) returns type T not T!?
                if (sourceExpression == null || sourceExpression.Kind != BoundKind.DefaultOperator || !destination.IsReferenceType || destination.GetEffectiveNullability() == EffectiveNullability.Nullable)
                    return Conversion.Identity;
                else
                    return Conversion.NoConversion;
            }

            Conversion conversion = ClassifyImplicitBuiltInConversionFromExpression(sourceExpression, source, destination, ref useSiteDiagnostics);
            if (conversion.Exists)
            {
                return conversion;
            }

            if ((object)source != null)
            {
                // Try using the short-circuit "fast-conversion" path.
                Conversion fastConversion = FastClassifyConversion(source, destination);
                if (fastConversion.Exists)
                {
                    return fastConversion.IsImplicit ? fastConversion : Conversion.NoConversion;
                }
                else
                {
                    conversion = ClassifyImplicitBuiltInConversionSlow(source, destination, ref useSiteDiagnostics);
                    if (conversion.Exists)
                    {
                        return conversion;
                    }
                }
            }

            return GetImplicitUserDefinedConversion(sourceExpression, source, destination, ref useSiteDiagnostics);
        }
开发者ID:jeffanders,项目名称:roslyn,代码行数:46,代码来源:Conversions.cs


注:本文中的TypeSymbol.GetEffectiveNullability方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。