Exemple #1
0
        private void InitializeNullableFlagsAndOriginalNullability(ref int nullableFlagsIndex, IEnumerable <dynamic> customAttributeProviders)
        {
            try
            {
                if (_nullableFlags == null)
                {
                    var nullableAttribute = ContextAttributes.FirstOrDefault(a => a.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
                    if (nullableAttribute != null)
                    {
                        _nullableFlags = GetFlagsFromNullableAttribute(nullableAttribute);
                    }
                    else if (customAttributeProviders != null)
                    {
                        _nullableFlags = GetFlagsFromCustomAttributeProviders(customAttributeProviders);
                    }
                    else
                    {
                        _nullableFlags = new byte[] { 0 };
                    }
                }
            }
            catch
            {
                _nullableFlags = new byte[] { 0 };
#if DEBUG
                throw;
#endif
            }

            if (OriginalType.GetTypeInfo().IsValueType)
            {
                OriginalNullability = Nullability.NotNullable;
            }
            else
            {
                var nullableFlag = _nullableFlags.Length > nullableFlagsIndex ? _nullableFlags[nullableFlagsIndex] : _nullableFlags.Last();
                nullableFlagsIndex++;

                OriginalNullability = nullableFlag == 0 ? Nullability.Unknown :
                                      nullableFlag == 1 ? Nullability.NotNullable :
                                      nullableFlag == 2 ? Nullability.Nullable :
                                      Nullability.Unknown;
            }
        }
        private void InitializeNullableFlagsAndOriginalNullability(ref int nullableFlagsIndex, IEnumerable <dynamic> customAttributeProviders)
        {
            try
            {
                if (_nullableFlags == null)
                {
                    var nullableAttribute = ContextAttributes.FirstOrDefault(a => a.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
                    if (nullableAttribute != null)
                    {
                        _nullableFlags = GetFlagsFromNullableAttribute(nullableAttribute);
                    }
                    else if (customAttributeProviders != null)
                    {
                        _nullableFlags = GetFlagsFromCustomAttributeProviders(customAttributeProviders);
                    }
                    else
                    {
                        _nullableFlags = new byte[] { 0 };
                    }
                }
            }
            catch
            {
                _nullableFlags = new byte[] { 0 };
#if DEBUG
                throw;
#endif
            }

            if (OriginalType.GetTypeInfo() is var typeInfo && typeInfo.IsValueType)
            {
                if (typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() != typeof(Nullable <>))
                {
                    nullableFlagsIndex++;
                }

                OriginalNullability = Nullability.NotNullable;
            }
Exemple #3
0
        private void InitializeNullableFlagsAndOriginalNullability(ref int nullableFlagsIndex, IEnumerable <dynamic>?customAttributeProviders)
        {
            var typeInfo = OriginalType.GetTypeInfo();

            try
            {
                if (_nullableFlags == null)
                {
                    var nullableAttribute = ContextAttributes.FirstOrDefault(a => a.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
                    if (nullableAttribute is not null)
                    {
                        _nullableFlags = GetFlagsFromNullableAttribute(nullableAttribute);
                    }
                    else if (typeInfo.IsGenericParameter)
                    {
                        nullableAttribute = typeInfo.GetCustomAttributes().FirstOrDefault(a => a.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute");
                        if (nullableAttribute is not null)
                        {
                            _nullableFlags = GetFlagsFromNullableAttribute(nullableAttribute);
                        }
                        else
                        {
                            // Default nullability (NullableContextAttribute) from the context
                            _nullableFlags = GetFlagsFromCustomAttributeProviders(typeInfo.DeclaringType.IsNested ? new dynamic[] { typeInfo.DeclaringType, typeInfo.DeclaringType.DeclaringType } : new dynamic[] { typeInfo.DeclaringType });
                        }
                    }
                    else if (customAttributeProviders is not null)
                    {
                        // Default nullability (NullableContextAttribute) from the context
                        _nullableFlags = GetFlagsFromCustomAttributeProviders(customAttributeProviders);
                    }
                    else
                    {
                        _nullableFlags = new byte[] { 0 }; // Unknown
                    }
                }
            }
            catch
            {
                _nullableFlags = new byte[] { 0 };
#if DEBUG
                throw;
#endif
            }

            if (typeInfo.IsValueType)
            {
                if (typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() != typeof(Nullable <>))
                {
                    nullableFlagsIndex++;
                }

                OriginalNullability = Nullability.NotNullable;
            }
            else
            {
                var nullableFlag = _nullableFlags.Length > nullableFlagsIndex ? _nullableFlags[nullableFlagsIndex] : _nullableFlags.Last();
                nullableFlagsIndex++;

                OriginalNullability = nullableFlag == 0 ? Nullability.Unknown :
                                      nullableFlag == 1 ? Nullability.NotNullable :
                                      nullableFlag == 2 ? Nullability.Nullable :
                                      Nullability.Unknown;
            }
        }