Ejemplo n.º 1
0
        public object CreateObject(MappingInfo mappinginfo, object obj)
        {
            PropertyMappingInfo pmi = null;
            object val = null;

            for (int i = 0; i < mappinginfo.Properties.Count; i++)
            {
                pmi = (PropertyMappingInfo)mappinginfo.Properties[i];

                val = pmi.PropertyDefaultValue;
                try
                {
                    obj.GetType().GetProperty(pmi.PropertyName).SetValue(obj, val, null);
                }
                catch
                {
                    if (obj.GetType().GetProperty(pmi.PropertyName).PropertyType.Equals(typeof(System.Enum)))
                    {
                        obj.GetType().GetProperty(pmi.PropertyName).SetValue(obj,
                                                                             System.Enum.ToObject(obj.GetType().GetProperty(pmi.PropertyName).PropertyType, val), null);
                    }
                    else
                    {
                        obj.GetType().GetProperty(pmi.PropertyName).SetValue(obj,
                                                                             Convert.ChangeType(val, obj.GetType().GetProperty(pmi.PropertyName).PropertyType), null);
                    }
                }
            }

            return(obj);
        }
Ejemplo n.º 2
0
        public T CreateObject <T>(SqlCommand command, MappingInfo mappinginfo) where T : class, new()
        {
            T obj = new T();

            PropertyMappingInfo pmi = null;
            object val         = null;
            bool   stringOrNot = false;

            string fieldName = "";

            //从数据库中读取出的列为reader.FieldCount
            for (int j = 0; j < command.Parameters.Count; j++)
            {
                if (command.Parameters[j].Direction != ParameterDirection.Output)
                {
                    continue;
                }

                fieldName = command.Parameters[j].ParameterName.ToLower().Trim().Replace("@", "");

                for (int i = 0; i < mappinginfo.Properties.Count; i++)
                {
                    pmi = (PropertyMappingInfo)mappinginfo.Properties[i];

                    if (pmi.ColumnName.ToLower().Trim() != fieldName)
                    {
                        continue;
                    }

                    if (command.Parameters[j].Value != DBNull.Value)
                    {
                        val = command.Parameters[j].Value;
                    }
                    else
                    {
                        val = pmi.PropertyDefaultValue;
                    }
                    PropertyInfo ppp = obj.GetType().GetProperty(pmi.PropertyName);
                    if (ppp == null)
                    {
                        continue;
                    }
                    if (obj.GetType().GetProperty(pmi.PropertyName).PropertyType.Equals(typeof(System.Enum)))
                    {
                        obj.GetType().GetProperty(pmi.PropertyName).SetValue(obj,
                                                                             System.Enum.ToObject(obj.GetType().GetProperty(pmi.PropertyName).PropertyType, val), null);
                    }
                    else
                    {
                        obj.GetType().GetProperty(pmi.PropertyName).SetValue(obj,
                                                                             Convert.ChangeType(val, obj.GetType().GetProperty(pmi.PropertyName).PropertyType), null);
                    }
                }
            }

            return(obj);
        }
Ejemplo n.º 3
0
        public T CreateObject <T>(IDataReader reader, MappingInfo mappinginfo) where T : class, new()
        {
            T obj = new T();

            PropertyMappingInfo pmi = null;
            object val         = null;
            bool   stringOrNot = false;

            string fieldName = "";

            //从数据库中读取出的列为reader.FieldCount
            for (int j = 0; j < reader.FieldCount; j++)
            {
                fieldName = reader.GetName(j).ToLower().Trim();

                for (int i = 0; i < mappinginfo.Properties.Count; i++)
                {
                    pmi = (PropertyMappingInfo)mappinginfo.Properties[i];

                    if (pmi.ColumnName.ToLower().Trim() != fieldName)
                    {
                        continue;
                    }

                    if (reader[pmi.ColumnName] != DBNull.Value)
                    {
                        val = reader[pmi.ColumnName];
                    }
                    else
                    {
                        val = pmi.PropertyDefaultValue;
                    }
                    PropertyInfo ppp = obj.GetType().GetProperty(pmi.PropertyName);
                    if (ppp == null)
                    {
                        continue;
                    }
                    if (obj.GetType().GetProperty(pmi.PropertyName).PropertyType.Equals(typeof(System.Enum)))
                    {
                        obj.GetType().GetProperty(pmi.PropertyName).SetValue(obj,
                                                                             System.Enum.ToObject(obj.GetType().GetProperty(pmi.PropertyName).PropertyType, val), null);
                    }
                    else
                    {
                        obj.GetType().GetProperty(pmi.PropertyName).SetValue(obj,
                                                                             Convert.ChangeType(val, obj.GetType().GetProperty(pmi.PropertyName).PropertyType), null);
                    }
                }
            }


            return(obj);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 从XML文件中加载映射信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private MappingInfo loadMappinInfoFromXml(Type type)
        {
            string name = type.Name;

            PropertyInfo[] properties = type.GetProperties();
            if (properties == null)
            {
                return(null);
            }
            MappingInfo         mappingInfo = new MappingInfo();
            PropertyMappingInfo pmi;

            foreach (PropertyInfo pi in properties)
            {
                ColumnMappingAttribute columnMappingAttribute = (ColumnMappingAttribute)Attribute.GetCustomAttribute(pi, typeof(ColumnMappingAttribute));
                if (columnMappingAttribute == null)
                {
                    continue;
                }
                pmi = new PropertyMappingInfo();
                pmi.PropertyName         = pi.Name;
                pmi.ColumnName           = columnMappingAttribute.ColumnName;
                pmi.PropertyDefaultValue = columnMappingAttribute.DefaultValue;
                mappingInfo.Properties.Add(pmi);
                mappingInfo.PropertiesHashTable.Add(pmi.PropertyName.ToLower(), pmi);
            }

            //MethodInfo[] methods = type.GetMethods();
            ////type.GetMethods(BindingFlags.FlattenHierarchy);
            //if (methods == null)
            //    return null;
            //OperationMappingInfo operationMappingInfo = null;
            //foreach (MethodInfo mi in methods)
            //{
            //    StorageProcedureMappingAttribute storageProcedureMappingAttribute = (StorageProcedureMappingAttribute)Attribute.GetCustomAttribute(mi, typeof(StorageProcedureMappingAttribute));
            //    if (storageProcedureMappingAttribute == null)
            //    {
            //        continue;
            //    }
            //    operationMappingInfo = new OperationMappingInfo();
            //    operationMappingInfo.StorageProcedure = storageProcedureMappingAttribute.StorageProcedureName;
            //    operationMappingInfo.PropertyNames = storageProcedureMappingAttribute.PropertyNames;
            //    mappingInfo.Methods.Add(operationMappingInfo);
            //}

            return(mappingInfo);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 获取一个默认实体集,可用于测试实体错误及显示表头
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List <T> FillDefaulValueRow <T>()
            where T : class, new()
        {
            T                   entity     = new T();
            XMLMapping          xmlMapping = new XMLMapping();
            Type                type       = entity.GetType();
            MappingInfo         mapInfo    = xmlMapping.GetDataMapInfo(type);
            PropertyMappingInfo pmi        = null;
            List <T>            entities   = new List <T>();

            for (int i = 0; i < mapInfo.Properties.Count; i++)
            {
                pmi = (PropertyMappingInfo)mapInfo.Properties[i];
                object propertyObj = Convert.ChangeType(pmi.PropertyDefaultValue, entity.GetType().GetProperty(pmi.PropertyName).PropertyType);
                entity.GetType().GetProperty(pmi.PropertyName).SetValue(entity, propertyObj, null);
            }

            entities.Add(entity);
            return(entities);
        }