本文整理汇总了C#中System.Type.IsNullableType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsNullableType方法的具体用法?C# Type.IsNullableType怎么用?C# Type.IsNullableType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.IsNullableType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NumberJsonSerializer
private NumberJsonSerializer(Type type, bool encrypt)
{
_encrypt = encrypt;
_nullable = type.IsNullableType();
_type = type;
SetDelegates(out _write, out _read);
}
示例2: Parse
public object Parse(string value, Type targetType)
{
if (targetType == null) throw new ArgumentNullException(nameof(targetType));
if(value == null)
{
if (targetType.IsNullableType())
return null;
throw new ArgumentException($"Cannot parse null value as type {targetType}");
}
targetType = Nullable.GetUnderlyingType(targetType) ?? targetType;
try
{
if(!CanParseType(targetType))
throw new InvalidOperationException($"Cannot parse value, \"{value}\", and type, \"{targetType.FullName}\"");
var enumValue = Enum.Parse(targetType, value);
if(!Enum.IsDefined(targetType, enumValue))
throw new ArgumentException($"The enum, \"{targetType}\" does not define a value for \"{value}\"");
return enumValue;
}
catch (ArgumentException)
{
throw new ArgumentOutOfRangeException(
$"The value, \"{value}\" does not exist for enumeration, \"{targetType.FullName}\"");
}
}
示例3: CreateReadValueExpression
public virtual Expression CreateReadValueExpression(Expression valueReader, Type type, int index)
{
Check.NotNull(valueReader, nameof(valueReader));
Check.NotNull(type, nameof(type));
var unwrappedTargetMemberType = type.UnwrapNullableType();
var underlyingTargetMemberType = unwrappedTargetMemberType.UnwrapEnumType();
var indexExpression = Expression.Constant(index);
Expression readValueExpression
= Expression.Call(
valueReader,
_readValue.MakeGenericMethod(underlyingTargetMemberType),
indexExpression);
if (underlyingTargetMemberType != type)
{
readValueExpression
= Expression.Convert(readValueExpression, type);
}
if (type.IsNullableType())
{
readValueExpression
= Expression.Condition(
Expression.Call(valueReader, _isNull, indexExpression),
Expression.Constant(null, type),
readValueExpression);
}
return readValueExpression;
}
示例4: InitDbParam
public override void InitDbParam(IDbDataParameter p, Type fieldType)
{
var sqlParam = (SqlParameter)p;
sqlParam.SqlDbType = SqlDbType.Udt;
sqlParam.IsNullable = fieldType.IsNullableType();
sqlParam.UdtTypeName = ColumnDefinition;
}
示例5: GetNullableType
internal static Type GetNullableType(Type type)
{
if (type.IsValueType && !type.IsNullableType())
{
return typeof(Nullable<>).MakeGenericType(new Type[] { type });
}
return type;
}
示例6: TryCreate
public static IPropertyEditor TryCreate(Type type)
{
if (type.IsNullableType())
{
return new NullablePropertyEditor(type);
}
return null;
}
示例7: ConditionalReceiver
public static ConditionalReceiver ConditionalReceiver(Type type)
{
ContractUtils.RequiresNotNull(type, nameof(type));
if (type == typeof(void) || type.IsByRef || type.IsNullableType())
{
throw Error.InvalidConditionalReceiverType(type);
}
return new ConditionalReceiver(type);
}
示例8: InitDbParam
public override void InitDbParam(IDbDataParameter p, Type fieldType)
{
if (fieldType == typeof(SqlHierarchyId))
{
var sqlParam = (SqlParameter)p;
sqlParam.IsNullable = fieldType.IsNullableType();
sqlParam.SqlDbType = SqlDbType.Udt;
sqlParam.UdtTypeName = ColumnDefinition;
}
base.InitDbParam(p, fieldType);
}
示例9: GetEnumerationType
public static Type GetEnumerationType(Type enumType)
{
if (enumType.IsNullableType())
{
enumType = enumType.GetGenericArguments()[0];
}
if (!enumType.IsEnum)
return null;
return enumType;
}
示例10: ConvertBack
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType != null && targetType.IsNullableType())
{
String strValue = value as String;
if (strValue == String.Empty)
{
return null;
}
}
return value;
}
示例11: DataRowFieldAccessExpressionBuilder
public DataRowFieldAccessExpressionBuilder(Type memberType, string memberName) : base(typeof(DataRow), memberName)
{
//Handle value types for null and DBNull.Value support converting them to Nullable<>
if (memberType.IsValueType && !memberType.IsNullableType())
{
this.columnDataType = typeof(Nullable<>).MakeGenericType(memberType);
}
else
{
this.columnDataType = memberType;
}
}
示例12: GetEnumerationType
public static Type GetEnumerationType(Type enumType)
{
if (enumType.IsNullableType())
{
enumType = enumType.GetTypeInfo().GenericTypeArguments[0];
}
if (!enumType.IsEnum())
return null;
return enumType;
}
示例13: TryCreate
public static IPropertyEditor TryCreate(Type type, ICustomAttributeProvider attributes)
{
if (type.IsNullableType()) {
return new NullablePropertyEditor(type.GetGenericArguments()[0]);
}
if (attributes != null &&
type.IsClass && attributes.IsDefined(typeof(InspectorNullableAttribute), /*inherit:*/true)) {
return new NullablePropertyEditor(type);
}
return null;
}
示例14: ChangeType
public static object ChangeType(object value, Type conversionType, CultureInfo cultureInfo) {
if (value == DBNull.Value)
value = null;
if (value == null || value.Equals("")) {
if (conversionType == typeof(DateTime))
return typeof(Nullable).IsAssignableFrom(conversionType) ? (object)null : DateTime.MinValue;
if (conversionType == typeof(int) || conversionType == typeof(double))
return typeof(Nullable).IsAssignableFrom(conversionType) ? (object)null : 0;
if (conversionType == typeof(bool))
return typeof(Nullable).IsAssignableFrom(conversionType) ? (object)null : false;
if (typeof(IEnumerable).IsAssignableFrom(conversionType) && string.IsNullOrEmpty(value + ""))
return null;
if (conversionType.IsValueType)
return conversionType.CreateInstance();
} else if (typeof(Enum).IsAssignableFrom(conversionType))
return Enum.Parse(conversionType, (string)value);
else if ((value + "").IsGuid() && conversionType == typeof(Guid))
return new Guid(value.ToString());
else if (value.GetType() == conversionType)
return value;
else {
var o = value as XPBaseObject;
if (o != null) {
if (conversionType == typeof(int))
return o.ClassInfo.KeyProperty.GetValue(o);
if (conversionType == typeof(string))
return o.ClassInfo.KeyProperty.GetValue(o).ToString();
return value;
}
if (conversionType == typeof(DateTime)) {
if ((value + "").Length > 0) {
var val = (value + "").Val();
if (val > 0)
return new DateTime(val);
}
} else if (value.GetType() != conversionType) {
if (conversionType.IsNullableType()) {
return ChangeType(value, conversionType.GetGenericArguments()[0], cultureInfo);
}
if (conversionType.IsGenericType) {
return value;
}
}
}
return Convert.ChangeType(value, conversionType, cultureInfo);
}
示例15: GetPropertyType
private Type GetPropertyType(Type memberType)
{
var descriptorProviderPropertyType = this.GetPropertyTypeFromTypeDescriptorProvider();
if (descriptorProviderPropertyType != null)
{
memberType = descriptorProviderPropertyType;
}
//Handle value types for null and DBNull.Value support converting them to Nullable<>
if (memberType.IsValueType && !memberType.IsNullableType())
{
return typeof(Nullable<>).MakeGenericType(memberType);
}
return memberType;
}
开发者ID:akhuang,项目名称:Asp.net-MVC-3,代码行数:16,代码来源:CustomTypeDescriptorPropertyAccessExpressionBuilder.cs