当前位置: 首页>>代码示例>>C#>>正文


C# Type.IsNullableType方法代码示例

本文整理汇总了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);
 }
开发者ID:QuickenLoans,项目名称:XSerializer,代码行数:7,代码来源:NumberJsonSerializer.cs

示例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}\"");
            }
        }
开发者ID:bitpantry,项目名称:BitPantry.Parsing.Strings,代码行数:32,代码来源:EnumParser.cs

示例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;
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:32,代码来源:EntityMaterializerSource.cs

示例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;
 }
开发者ID:chrisklepeis,项目名称:ServiceStack.OrmLite,代码行数:7,代码来源:SqlServerTypeConverter.cs

示例5: GetNullableType

 internal static Type GetNullableType(Type type)
 {
     if (type.IsValueType && !type.IsNullableType())
     {
         return typeof(Nullable<>).MakeGenericType(new Type[] { type });
     }
     return type;
 }
开发者ID:nickchal,项目名称:pash,代码行数:8,代码来源:TypeUtils.cs

示例6: TryCreate

 public static IPropertyEditor TryCreate(Type type)
 {
     if (type.IsNullableType())
     {
         return new NullablePropertyEditor(type);
     }
     return null;
 }
开发者ID:fuboss,项目名称:aiProject,代码行数:8,代码来源:NullablePropertyEditor.cs

示例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);
        }
开发者ID:taolin123,项目名称:ExpressionFutures,代码行数:11,代码来源:ConditionalReceiver.cs

示例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);
 }
开发者ID:ServiceStack,项目名称:ServiceStack.OrmLite,代码行数:11,代码来源:SqlServerHierarchyIdTypeConverter.cs

示例9: GetEnumerationType

        public static Type GetEnumerationType(Type enumType)
        {
            if (enumType.IsNullableType())
            {
                enumType = enumType.GetGenericArguments()[0];
            }

            if (!enumType.IsEnum)
                return null;

            return enumType;
        }
开发者ID:JonKruger,项目名称:AutoMapper,代码行数:12,代码来源:TypeHelper.cs

示例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;
 }
开发者ID:expanz,项目名称:expanz-Microsoft-XAML-SDKs,代码行数:12,代码来源:DataGridValueConverter.cs

示例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;
     }
 }
开发者ID:akhuang,项目名称:Asp.net-MVC-3,代码行数:12,代码来源:DataRowFieldAccessExpressionBuilder.cs

示例12: GetEnumerationType

        public static Type GetEnumerationType(Type enumType)
        {
            if (enumType.IsNullableType())
            {
                enumType = enumType.GetTypeInfo().GenericTypeArguments[0];
            }

            if (!enumType.IsEnum())
                return null;

            return enumType;
        }
开发者ID:tamirdresher,项目名称:AutoMapper,代码行数:12,代码来源:TypeHelper.cs

示例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;
        }
开发者ID:JoeYarnall,项目名称:something-new,代码行数:13,代码来源:NullablePropertyEditor.cs

示例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);

        }
开发者ID:aries544,项目名称:eXpand,代码行数:48,代码来源:XpandReflectionHelper.cs

示例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


注:本文中的System.Type.IsNullableType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。