当前位置: 首页>>代码示例>>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;未经允许,请勿转载。