本文整理汇总了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);
}
}
示例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();
}
示例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);
}
示例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);
}
示例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));
}
}
示例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;
}
示例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});
}
示例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));
}
示例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;
}
示例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);
}
示例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." );
}
}
示例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");
}
示例13: GetPgType
public static string GetPgType(Type memberType)
{
if (memberType.IsEnum) return "integer";
if (memberType.IsNullable())
{
return GetPgType(memberType.GetInnerTypeFromNullable());
}
return PgTypes[memberType];
}
示例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);
}
}
示例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());
}