示例#1
0
        /// <summary>
        /// 转换为共同属性的集合
        /// </summary>
        /// <typeparam name="TDest"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List <TDest> ToType <TDest>(this IEnumerable source)
            where TDest : class, new()
        {
            var simpleTypes = typeof(TDest).GetProperties();
            List <PropertyInfo> complexTypes = null;
            List <TDest>        list         = new List <TDest>();

            foreach (var item in source)
            {
                TDest obj = new TDest();
                if (complexTypes == null)
                {
                    complexTypes = item.GetType().GetProperties().ToList();
                    complexTypes.RemoveAll(b => b.Name == "Item");
                }
                foreach (var info in simpleTypes)
                {
                    var complexInfo = complexTypes.Find(b => b.Name == info.Name);
                    if (complexInfo != null)
                    {
                        object value = complexInfo.GetValue(item, null);
                        value = ObjectConvert.ConvertObject(info.PropertyType, value);
                        info.SetValue(obj, value, null);
                    }
                }
                list.Add(obj);
            }
            return(list);
        }
示例#2
0
        /// <summary>
        /// 指定替换对象返回单个结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public T ExecScalar <T>(string sql, params Type[] types)
        {
            sql = _DBAdapter.SqlFormat(sql);
            var obj = ExecScalar(sql, types);

            return(ObjectConvert.ConvertObject <T>(obj));
        }
示例#3
0
        /// <summary>
        /// 返回首行首列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public T AutoExecuteScalar <T>(string sql, params Type[] types)
        {
            object obj;

            sql = _DBAdapter.SqlFormat(sql);
            sql = AutoFormat(sql, types);
            sql = _DBAdapter.SqlFormat(sql);
            string sp = CompileSqlToSp(_DBAdapter.TemplateSp, sql);

            try
            {
                obj = RunScalar(sp);
            }
            catch (Exception ero)
            {
                if (ero.Message.Contains("找不到存储过程"))//删除后自动创建
                {
                    spCahe.Remove(sp);
                    sp  = CompileSqlToSp(_DBAdapter.TemplateSp, sql);
                    obj = RunScalar(sp);
                }
                throw ero;
            }
            ClearParame();
            return(ObjectConvert.ConvertObject <T>(obj));
        }
示例#4
0
        /// <summary>
        /// 返回首行首列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public T AutoExecuteScalar <T>(string sql, params Type[] types)
        {
            object obj;

            sql = _DBAdapter.SqlFormat(sql);
            sql = AutoFormat(sql, types);
            sql = _DBAdapter.SqlFormat(sql);
            string sp = CompileSqlToSp(_DBAdapter.TemplateSp, sql);

            obj = RunScalar(sp);
            ClearParame();
            return(ObjectConvert.ConvertObject <T>(obj));
        }
示例#5
0
文件: Convert.cs 项目: zyj0021/CRL5
        static object ToType(Dictionary <string, PropertyInfo> sourceTypes, Dictionary <string, PropertyInfo> destTypes, object source, Type toType)
        {
            if (source == null)
            {
                return(null);
            }
            object obj;

            //obj = System.Activator.CreateInstance(toType);
            try
            {
                obj = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(toType);
            }
            catch
            {
                throw new CRLException(string.Format("{1}不能转换为{0},请检查属性定义", toType, source.GetType()));
            }
            foreach (var kv in destTypes)
            {
                var          key  = kv.Key;
                var          info = kv.Value;
                PropertyInfo sourceInfo;
                var          a = sourceTypes.TryGetValue(key, out sourceInfo);
                if (!a)
                {
                    continue;
                }

                object value;
                var    nameSpace = sourceInfo.PropertyType.Namespace;
                if (nameSpace == "System" || sourceInfo.PropertyType.BaseType == typeof(Enum))
                {
                    value = sourceInfo.GetValue(source, null);
                    value = ObjectConvert.ConvertObject(info.PropertyType, value);
                }
                else//如果是class,则再转换一次
                {
                    object value2 = sourceInfo.GetValue(source, null);
                    if (value2 == null)
                    {
                        continue;
                    }
                    var sourceTypes2 = GetObjProperty(sourceInfo.PropertyType);
                    var destTypes2   = GetObjProperty(info.PropertyType);
                    value = ToType(sourceTypes2, destTypes2, value2, info.PropertyType);
                }
                info.SetValue(obj, value, null);
            }
            return(obj);
        }
示例#6
0
        static object ToType(IEnumerable <PropertyInfo> sourceTypes, IEnumerable <PropertyInfo> destTypes, object source, Type toType)
        {
            if (source == null)
            {
                return(null);
            }
            object obj;

            //obj = System.Activator.CreateInstance(toType);
            try
            {
                obj = System.Activator.CreateInstance(toType);
            }
            catch
            {
                throw new CRLException(string.Format("{1}不能转换为{0},请检查属性定义", toType, source.GetType()));
            }
            foreach (var info in destTypes)
            {
                var sourceInfo = sourceTypes.Find(b => b.Name.ToLower() == info.Name.ToLower());
                if (sourceInfo != null)
                {
                    object value;
                    var    nameSpace = sourceInfo.PropertyType.Namespace;
                    if (nameSpace == "System" || sourceInfo.PropertyType.BaseType == typeof(Enum))
                    {
                        value = sourceInfo.GetValue(source, null);
                        value = ObjectConvert.ConvertObject(info.PropertyType, value);
                    }
                    else//如果是class,则再转换一次
                    {
                        object value2 = sourceInfo.GetValue(source, null);
                        if (value2 == null)
                        {
                            continue;
                        }
                        var sourceTypes2 = sourceInfo.PropertyType.GetProperties().ToList();
                        sourceTypes2.RemoveAll(b => b.SetMethod == null || (b.SetMethod != null && b.SetMethod.Name == "set_Item"));
                        var destTypes2 = info.PropertyType.GetProperties().ToList();
                        destTypes2.RemoveAll(b => b.SetMethod == null || (b.SetMethod != null && b.SetMethod.Name == "set_Item"));
                        value = ToType(sourceTypes2, destTypes2, value2, info.PropertyType);
                    }
                    info.SetValue(obj, value, null);
                }
            }
            return(obj);
        }
示例#7
0
        /// <summary>
        /// 转换共同属性的对象
        /// </summary>
        /// <typeparam name="TDest"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static TDest ToType <TDest>(this object source)
            where TDest : class, new()
        {
            var simpleTypes = typeof(TDest).GetProperties();
            List <PropertyInfo> complexTypes = source.GetType().GetProperties().ToList();

            complexTypes.RemoveAll(b => b.Name == "Item");
            TDest obj = new TDest();

            foreach (var info in simpleTypes)
            {
                var complexInfo = complexTypes.Find(b => b.Name == info.Name);
                if (complexInfo != null)
                {
                    object value = complexInfo.GetValue(source, null);
                    value = ObjectConvert.ConvertObject(info.PropertyType, value);
                    info.SetValue(obj, value, null);
                }
            }
            return(obj);
        }
示例#8
0
        public T GetOutParam <T>(string name)
        {
            var obj = helper.GetOutParam(name);

            return(ObjectConvert.ConvertObject <T>(obj));
        }