當前位置: 首頁>>代碼示例>>C#>>正文


C# Type.IsNullable方法代碼示例

本文整理匯總了C#中System.Type.IsNullable方法的典型用法代碼示例。如果您正苦於以下問題:C# Type.IsNullable方法的具體用法?C# Type.IsNullable怎麽用?C# Type.IsNullable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Type的用法示例。


在下文中一共展示了Type.IsNullable方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: GenerateCanEvalutateManyTimesTest

        public void GenerateCanEvalutateManyTimesTest(Type type, bool typeSupported, double min, double max)
        {
            if (typeSupported == false)
            {
                // Ignore this test
                return;
            }

            var target = new NumericValueGenerator();

            for (var index = 0; index < 10000; index++)
            {
                var value = target.Generate(type, null, null);
                
                if (type.IsNullable() && value == null)
                {
                    // Nullable values could be returned so nothing more to assert
                    return;
                }

                var evaluateType = type;

                if (type.IsNullable())
                {
                    evaluateType = type.GenericTypeArguments[0];
                }

                value.Should().BeOfType(evaluateType);

                var convertedValue = Convert.ToDouble(value);

                convertedValue.Should().BeGreaterOrEqualTo(min);
                convertedValue.Should().BeLessOrEqualTo(max);
            }
        }
開發者ID:roryprimrose,項目名稱:ModelBuilder,代碼行數:35,代碼來源:NumericValueGeneratorTests.cs

示例2: GetComparator

        internal static Comparator GetComparator(Type type)
        {
            if (type.IsNullable<DateTime>())
            {
                return new NullableDateComparator();
            }

            if (type == typeof (DateTime))
            {
                return new DateComparator();
            }

            if (type == typeof (string))
            {
                return new StringComparator();
            }

            if (type.IsNullable())
            {
                return new NullableComparator();
            }

            if (type.IsValueType)
            {
                return new ValueTypeComparator();
            }

            return new Comparator();
        }
開發者ID:JensKlambauer,項目名稱:tracker-enabled-dbcontext,代碼行數:29,代碼來源:ComparatorFactory.cs

示例3: CreateSpecFor

        private ModelSpec CreateSpecFor(Type type, bool deferIfComplex, Dictionary<Type, ModelSpec> deferredMappings)
        {
            if (_customMappings.ContainsKey(type))
                return _customMappings[type];

            if (PrimitiveMappings.ContainsKey(type))
                return PrimitiveMappings[type];

            if (type.IsEnum)
                return new ModelSpec {Type = "string", Enum = type.GetEnumNames()};

            Type innerType;
            if (type.IsNullable(out innerType))
                return CreateSpecFor(innerType, deferIfComplex, deferredMappings);

            Type itemType;
            if (type.IsEnumerable(out itemType))
                return new ModelSpec { Type = "array", Items = CreateSpecFor(itemType, true, deferredMappings) };

            // Anthing else is complex

            if (deferIfComplex)
            {
                if (!deferredMappings.ContainsKey(type))
                    deferredMappings.Add(type, null);
                
                // Just return a reference for now
                return new ModelSpec {Ref = UniqueIdFor(type)};
            }

            return CreateComplexSpecFor(type, deferredMappings);
        }
開發者ID:robinvanderknaap,項目名稱:Swashbuckle.Lite,代碼行數:32,代碼來源:ModelSpecGenerator.cs

示例4: CanConvertTo

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType.IsNullable())
                return CanConvertTo(context, Nullable.GetUnderlyingType(destinationType));

            return GoodTypes.Contains(destinationType) || base.CanConvertTo(context, destinationType);
        }
開發者ID:thomas-parrish,項目名稱:Common,代碼行數:7,代碼來源:EnumConverter.cs

示例5: IsSameType

 /// <summary>
 /// Checks that a type is the same as the expected one.
 /// </summary>
 /// <param name="instanceType">The type of the instance to be checked.</param>
 /// <param name="expectedType">The expected type for the instance to be checked.</param>
 /// <param name="value">The value of the instance to be checked (may be a nullable instance).</param>
 public static void IsSameType(Type instanceType, Type expectedType, object value)
 {
     if (instanceType != expectedType || (value == null && !instanceType.IsNullable()))
     {
         throw new FluentCheckException(BuildErrorMessageForNullable(instanceType, expectedType, value, false));
     }
 }
開發者ID:dirkrombauts,項目名稱:NFluent,代碼行數:13,代碼來源:IsInstanceHelper.cs

示例6: TsType

        /// <summary>
        /// Initializes a new instance of the TsType class with the specific CLR type.
        /// </summary>
        /// <param name="type">The CLR type represented by this instance of the TsType.</param>
        public TsType(Type type)
        {
            if (type.IsNullable()) {
                type = type.GetNullableValueType();
            }

            this.Type = type;
        }
開發者ID:spelltwister,項目名稱:KnockoutTypeLITE,代碼行數:12,代碼來源:TsType.cs

示例7: ToDbType

        public static NpgsqlDbType ToDbType(Type type)
        {
            if (type.IsNullable()) return ToDbType(type.GetInnerTypeFromNullable());

            if (type == typeof (DateTime)) return NpgsqlDbType.Date;

            return (NpgsqlDbType) _getNgpsqlDbTypeMethod.Invoke(null, new object[] {type});
        }
開發者ID:brianstanforth,項目名稱:marten,代碼行數:8,代碼來源:TypeMappings.cs

示例8: IsPrimitiveType

        public bool IsPrimitiveType(Type type)
        {
            if (type == null)
                return false;

            if (type.IsNullable())
                type = Nullable.GetUnderlyingType(type);

            return type.IsPrimitive || type.IsEnum || primitiveTypes.Any(x => x.IsAssignableFrom(type));
        }
開發者ID:Kryptos-FR,項目名稱:xenko-reloaded,代碼行數:10,代碼來源:DefaultNodeBuilder.cs

示例9: TryGetPerTypeBehavior

 /// <summary>
 /// Extension point. Try to find per-type <see cref="ReplicationBehavior"/> or
 /// return <c>null</c>, if current <see cref="IMetadataProvider"/> does not specify it.
 /// </summary>
 protected virtual ReplicationBehavior? TryGetPerTypeBehavior(Type entityType)
 {
     if (entityType.IsPrimitive || entityType.IsEnum || entityType == typeof (string))
         return ReplicationBehavior.Copy;
     if (entityType.IsNullable()) {
         Type underlyingType = entityType.GetNullableUnderlyingType();
         return GetPerTypeBehavior(underlyingType);
     }
     return null;
 }
開發者ID:saeedallahyari,項目名稱:NClone,代碼行數:14,代碼來源:DefaultMetadataProvider.cs

示例10: ConvertTo

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType.IsNullable() && value != null)
                return ConvertTo(context, culture, value, Nullable.GetUnderlyingType(destinationType));

            if (GoodTypes.Contains(destinationType))
                return Convert.ChangeType(value, destinationType);

            return base.ConvertTo(context, culture, value, destinationType);
        }
開發者ID:thomas-parrish,項目名稱:Common,代碼行數:10,代碼來源:EnumConverter.cs

示例11: ReadJson

 public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer ) {
     var jsonValue = serializer.Deserialize<JValue>(reader);
     switch ( jsonValue.Type ) {
         case JTokenType.Integer:
             var dto = DateTimeOffsetHelper.FromUnixTimeSeconds( jsonValue.Value<long>() );
             if ( objectType == DateTimeOffsetType || ( objectType.IsNullable() && Nullable.GetUnderlyingType( objectType ) == DateTimeOffsetType ) )
                 return dto;
             return dto.LocalDateTime;
         case JTokenType.Date:
             if ( objectType == DateTimeOffsetType || (objectType.IsNullable() && Nullable.GetUnderlyingType(objectType) == DateTimeOffsetType))
                 return jsonValue.Value<DateTimeOffset>();
             return jsonValue.Value<DateTime>();
         case JTokenType.Null:
             if ( objectType.IsNullable() )
                 return null;
             throw new JsonSerializationException( $"Can't deserialize null value to non-nullable type" );
         default:
             throw new JsonSerializationException( $"Unexpected token {jsonValue.Type} when parsing a date." );
     }
 }
開發者ID:kasthack,項目名稱:kasthack.vksharp,代碼行數:20,代碼來源:UnixTimeConverter.cs

示例12: ToType

        public static object ToType(this string origin, Type dest)
        {
            if (dest.IsNullable() && string.IsNullOrEmpty(origin))
                return null;

            var realType = dest.GetValueTypeIfNullable();
            if (typeof(IConvertible).IsAssignableFrom(realType))
                return Convert.ChangeType(origin, realType);

            throw new NotSupportedException("Cannot convert to type");
        }
開發者ID:jonathascosta,項目名稱:SystemExtensions,代碼行數:11,代碼來源:StringExtensions.cs

示例13: GetPgType

        public static string GetPgType(Type memberType)
        {
            if (memberType.IsEnum) return "integer";

            if (memberType.IsNullable())
            {
                return GetPgType(memberType.GetInnerTypeFromNullable());
            }

            return PgTypes[memberType];
        }
開發者ID:brianstanforth,項目名稱:marten,代碼行數:11,代碼來源:TypeMappings.cs

示例14: DataRowMapper

 public DataRowMapper(Type toType)
     : base(typeof(DataRow), toType)
 {
     if (!toType.IsAbstract)
     {
         if (toType.IsNullable())
             Ctor = DynamicMethodFactory.GetDefaultCreator(Nullable.GetUnderlyingType(toType));
         else
             Ctor = DynamicMethodFactory.GetDefaultCreator(toType);
     }
 }
開發者ID:netcasewqs,項目名稱:nlite,代碼行數:11,代碼來源:DataRowMapper.cs

示例15: BuildDataType

        public static DataType BuildDataType(Type type, Func<Type, MemberInfo, int> membersOrder = null)
        {
            if (DataType.IsPrimitiveType(type) || type.IsEnum || type == typeof(Guid) || type.IsKeyValuePair() || type.IsArray || type.IsList() || type.IsDictionary() || type.IsNullable())
                return BuildDataType(type, membersOrder, new HashSet<Type>());

            List<DataType> slots = new List<DataType>();
            foreach (var member in GetPublicMembers(type, membersOrder))
                slots.Add(BuildDataType(member.GetPropertyOrFieldType(), membersOrder, new HashSet<Type>()));

            return DataType.Slots(slots.ToArray());
        }
開發者ID:KSLcom,項目名稱:STSdb4,代碼行數:11,代碼來源:DataTypeUtils.cs


注:本文中的System.Type.IsNullable方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。