Пример #1
0
        static DataDefine CreateMapping(Type type)
        {
            TypeInfo typeInfo   = type.GetTypeInfo();
            bool     isNullable = false;

            if (typeInfo.IsGenericType)
            {
                Type frameType = type.GetGenericTypeDefinition();
                if (frameType.FullName == "System.Nullable`1")
                {
                    Type[] arguments = typeInfo.GetGenericArguments();
                    type       = arguments[0];
                    typeInfo   = type.GetTypeInfo();
                    isNullable = true;
                }
            }

            DataDefine define;

            if (type.IsArray)
            {
                if (type.FullName == "System.Byte[]")
                {
                    define = new BytesDataDefine(type, true);
                }
                else
                {
                    throw new LightDataException(string.Format(SR.DataDefineUnsupportFieldType, type));
                }
            }
            else if (type.IsGenericParameter || typeInfo.IsGenericTypeDefinition)
            {
                throw new LightDataException(string.Format(SR.DataDefineUnsupportFieldType, type));
            }
            else if (typeInfo.IsEnum)
            {
                define = new EnumDataDefine(type, isNullable);
            }
            else
            {
                TypeCode code = Type.GetTypeCode(type);
                switch (code)
                {
                case TypeCode.Empty:
                case TypeCode.Object:
                    throw new LightDataException(string.Format(SR.DataDefineUnsupportFieldType, type));

                case TypeCode.String:
                    define = new PrimitiveDataDefine(type, true);
                    break;

                default:
                    define = new PrimitiveDataDefine(type, isNullable);
                    break;
                }
            }
            return(define);
        }
Пример #2
0
        private static DataDefine CreateMapping(Type type)
        {
            var typeInfo   = type.GetTypeInfo();
            var isNullable = false;

            if (typeInfo.IsGenericType)
            {
                var frameType = type.GetGenericTypeDefinition();
                if (frameType.FullName == "System.Nullable`1")
                {
                    var arguments = typeInfo.GetGenericArguments();
                    type       = arguments[0];
                    typeInfo   = type.GetTypeInfo();
                    isNullable = true;
                }
            }

            DataDefine define;

            if (type.IsArray)
            {
                if (type.FullName == "System.Byte[]")
                {
                    define = new BytesDataDefine(true);
                }
                else
                {
                    define = new ObjectDataDefine(type, true);
                }
            }
            // else if (type.IsGenericParameter || typeInfo.IsGenericTypeDefinition) {
            //     throw new LightDataException(string.Format(SR.DataDefineUnsupportFieldType, type));
            // }
            else if (type == typeof(Guid))
            {
                define = new GuidDataDefine(isNullable);
            }
            else if (type == typeof(string))
            {
                define = new StringDataDefine(true);
            }
            else if (type == typeof(DateTime))
            {
                define = new DateTimeDataDefine(isNullable);
            }
            else if (type == typeof(decimal))
            {
                define = new DecimalDataDefine(isNullable);
            }
            else if (typeInfo.IsEnum)
            {
                define = new EnumDataDefine(type, isNullable);
            }
            else if (type.IsPrimitive)
            {
                define = new PrimitiveDataDefine(type, isNullable);
            }
            else if (type == typeof(DBNull))
            {
                throw new LightDataException(string.Format(SR.DataDefineUnsupportFieldType, type));
            }
            else
            {
                define = new ObjectDataDefine(type, true);
            }
            return(define);
        }