Exemplo n.º 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);
        }
Exemplo n.º 2
0
        private static DataDefine TransferDataDefine(DataFieldMapping fieldMapping)
        {
            DataDefine define = null;

            if (fieldMapping is PrimitiveFieldMapping)
            {
                if (fieldMapping.ObjectType == typeof(string))
                {
                    define = PrimitiveDataDefine.CreateString(fieldMapping.IsNullable, fieldMapping.Name);
                }
                else
                {
                    if (fieldMapping.IsNullable)
                    {
                        Type itemstype = System.Type.GetType("System.Nullable`1");
                        Type type      = itemstype.MakeGenericType(fieldMapping.ObjectType);
                        define = PrimitiveDataDefine.Create(type, fieldMapping.Name);
                    }
                    else
                    {
                        define = PrimitiveDataDefine.Create(fieldMapping.ObjectType, fieldMapping.Name);
                    }
                }
            }
            else
            if (fieldMapping is EnumFieldMapping)
            {
                EnumFieldMapping em = fieldMapping as EnumFieldMapping;
                if (fieldMapping.IsNullable)
                {
                    Type itemstype = System.Type.GetType("System.Nullable`1");
                    Type type      = itemstype.MakeGenericType(fieldMapping.ObjectType);
                    define = EnumDataDefine.Create(type, em.EnumType, fieldMapping.Name);
                }
                else
                {
                    define = EnumDataDefine.Create(fieldMapping.ObjectType, em.EnumType, fieldMapping.Name);
                }
            }
            else
            {
                throw new LightDataException(RE.OnlyPrimitiveFieldCanSelectSingle);
            }
            return(define);
        }
Exemplo n.º 3
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);
        }