Пример #1
0
        void DataRowToEntity(MethodInformation method, DataRow dr, object entity)
        {
            if (null == dr)
            {
                return;
            }
            if (null == entity)
            {
                return;
            }

            object v = null;
            Dictionary <string, object> dic = new Dictionary <string, object>();

            foreach (DataColumn dc in dr.Table.Columns)
            {
                v = dr[dc.ColumnName];
                dic.Add(dc.ColumnName, v);
            }

            PropertyInfo[] piArr = entity.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo item in piArr)
            {
                if ((method.methodComponent.IsAsync || method.methodComponent.EnabledBuffer) &&
                    0 < method.methodComponent.Interval)
                {
                    Thread.Sleep(method.methodComponent.Interval);
                }
                v = null;
                dic.TryGetValue(item.Name, out v);
                if (null != v)
                {
                    if (DJTools.IsBaseType(item.PropertyType))
                    {
                        v = DJTools.ConvertTo(v, item.PropertyType);
                        try
                        {
                            item.SetValue(entity, v, null);
                        }
                        catch (Exception ex)
                        {
                            //throw;
                        }
                    }
                }
            }
        }
Пример #2
0
        static object getClone(object srcEntity, object entity, int clonePara, int while_num)
        {
            while_num++;
            object o = srcEntity;

            if (null == entity)
            {
                return(o);
            }
            Type t = entity.GetType();

            if (DJTools.IsBaseType(t))
            {
                return(DJTools.ConvertTo(entity, t));
            }

            Action <object, object> add_dic = (newObj, cloneObj) =>
            {
                object _o = null;
                _clone_dic.TryGetValue(newObj, out _o);
                if (null == _o)
                {
                    _clone_dic.Add(newObj, cloneObj);
                }
            };

            if (null == o)
            {
                Type t1 = entity.GetType();
                if (0 == clonePara && null != srcEntity)
                {
                    t1 = srcEntity.GetType();
                }

                try
                {
                    o = Activator.CreateInstance(t1);
                    if (0 == clonePara && null != srcEntity)
                    {
                        add_dic(srcEntity, o);
                    }
                    else
                    {
                        add_dic(entity, o);
                    }
                }
                catch { }
            }
            if (null == o)
            {
                return(o);
            }

            if (99 < while_num)
            {
                return(o);
            }

            Func <object, DicEle> SameFunc = (newObj) =>
            {
                object _oo = null;
                _clone_dic.TryGetValue(newObj, out _oo);
                dicEle.isSame = null != _oo;
                dicEle.nObj   = _oo;
                return(dicEle);
            };

            string       fName = "";
            object       fv    = null;
            DicEle       dic_Ele;
            PropertyInfo pi = null;
            Dictionary <string, PropertyInfo> dic = new Dictionary <string, PropertyInfo>();

            PropertyInfo[] arr = o.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo item in arr)
            {
                fv = null;
                if (item.CanRead)
                {
                    fv = item.GetValue(o, null);
                    if (null == item.PropertyType.GetInterface("IEnumerable"))
                    {
                        fv = null;
                    }
                    if (typeof(string) == item.PropertyType)
                    {
                        fv = null;
                    }
                }

                if (!item.CanWrite && null == fv)
                {
                    continue;
                }
                fName = item.Name.ToLower();
                pi    = null;
                dic.TryGetValue(fName, out pi);
                if (null != pi)
                {
                    continue;
                }
                dic.Add(fName, item);
            }

            arr = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo item in arr)
            {
                if (!item.CanRead)
                {
                    continue;
                }
                fName = item.Name.ToLower();
                pi    = null;
                dic.TryGetValue(fName, out pi);
                if (null == pi)
                {
                    continue;
                }

                fv = item.GetValue(entity, null);
                if (null == fv)
                {
                    continue;
                }

                if (null != item.PropertyType.GetInterface("IEnumerable"))
                {
                    if (item.PropertyType.IsArray)
                    {
                        Array array = fv as Array;
                        if (null == array)
                        {
                            continue;
                        }
                        int    len  = array.Length;
                        object arr1 = null;

                        if (pi.CanWrite)
                        {
                            arr1 = DJTools.createArrayByType(fv.GetType(), len);
                        }
                        else
                        {
                            arr1 = pi.GetValue(o, null);
                        }
                        if (null == arr1)
                        {
                            continue;
                        }

                        if (0 < len)
                        {
                            object ele = null;
                            int    n   = 0;
                            foreach (var a in array)
                            {
                                dic_Ele = SameFunc(a);
                                if (dic_Ele.isSame)
                                {
                                    ele = dic_Ele.nObj;
                                }
                                else
                                {
                                    ele = getClone(null, a, 1, while_num);
                                }
                                DJTools.arrayAdd(arr1, ele, n);
                                n++;
                            }
                        }

                        fv = arr1;
                    }
                    else if (null != item.PropertyType.GetInterface("IList"))
                    {
                        ICollection collection = fv as ICollection;
                        if (null == collection)
                        {
                            continue;
                        }
                        object list = null;
                        if (pi.CanWrite)
                        {
                            list = DJTools.createListByType(fv.GetType());
                        }
                        else
                        {
                            list = pi.GetValue(o, null);
                        }
                        if (null == list)
                        {
                            continue;
                        }

                        if (0 < collection.Count)
                        {
                            object ele = null;
                            foreach (var a in collection)
                            {
                                dic_Ele = SameFunc(a);
                                if (dic_Ele.isSame)
                                {
                                    ele = dic_Ele.nObj;
                                }
                                else
                                {
                                    ele = getClone(null, a, 1, while_num);
                                }
                                DJTools.listAdd(list, ele);
                            }
                        }

                        fv = list;
                    }
                    else if (null != item.PropertyType.GetInterface("IDictionary"))
                    {
                        IDictionary collection = fv as IDictionary;
                        if (null == collection)
                        {
                            continue;
                        }
                        object dic1 = null;
                        if (pi.CanWrite)
                        {
                            dic1 = DJTools.createDictionaryByType(fv.GetType());
                        }
                        else
                        {
                            dic1 = pi.GetValue(o, null);
                        }
                        if (null == dic1)
                        {
                            continue;
                        }

                        if (0 < collection.Count)
                        {
                            string key = "";
                            object val = null;
                            object ele = null;
                            foreach (var a in collection)
                            {
                                key = a.GetType().GetProperty("Key").GetValue(item, null).ToString();
                                val = a.GetType().GetProperty("Value").GetValue(item, null);
                                if (null != val)
                                {
                                    dic_Ele = SameFunc(val);
                                    if (dic_Ele.isSame)
                                    {
                                        ele = dic_Ele.nObj;
                                    }
                                    else
                                    {
                                        ele = getClone(null, val, 1, while_num);
                                    }
                                }
                                DJTools.dictionaryAdd(dic1, key, val);
                            }
                        }

                        fv = dic1;
                    }
                }
                else if (item.PropertyType.IsClass)
                {
                    dic_Ele = SameFunc(fv);
                    if (dic_Ele.isSame)
                    {
                        fv = dic_Ele.nObj;
                    }
                    else
                    {
                        fv = getClone(null, fv, 1, while_num);
                    }
                }
                else if (DJTools.IsBaseType(item.PropertyType))
                {
                    fv = DJTools.ConvertTo(fv, item.PropertyType);
                }

                if (pi.CanWrite)
                {
                    try
                    {
                        pi.SetValue(o, fv, null);
                    }
                    catch { }
                }
            }

            return(o);
        }
Пример #3
0
        object dataTableTo(MethodInformation method, DataTable dt, Type paraType)
        {
            object result = null;

            if (null == dt)
            {
                return(result);
            }
            if (0 == dt.Rows.Count)
            {
                return(result);
            }

            object v = null;

            if (DJTools.IsBaseType(paraType))
            {
                v = dt.Rows[0][0];
                v = DJTools.ConvertTo(v, paraType);
            }
            else if (typeof(DataEntity <DataElement>) == paraType)
            {
                DataEntity <DataElement> dataElements = new DataEntity <DataElement>();
                DataRow dr = dt.Rows[0];
                DataColumnCollection columns = dt.Columns;
                foreach (DataColumn item in columns)
                {
                    if ((method.methodComponent.IsAsync || method.methodComponent.EnabledBuffer) &&
                        0 < method.methodComponent.Interval)
                    {
                        Thread.Sleep(method.methodComponent.Interval);
                    }
                    v = dr[item.ColumnName];
                    dataElements.Add(item.ColumnName, v);
                }
                v = dataElements;
            }
            else if (typeof(List <DataEntity <DataElement> >) == paraType || typeof(IList <DataEntity <DataElement> >) == paraType)
            {
                List <DataEntity <DataElement> > dataElements  = new List <DataEntity <DataElement> >();
                DataEntity <DataElement>         dataElements1 = null;
                DataColumnCollection             columns       = dt.Columns;
                foreach (DataRow dr in dt.Rows)
                {
                    if ((method.methodComponent.IsAsync || method.methodComponent.EnabledBuffer) &&
                        0 < method.methodComponent.Interval)
                    {
                        Thread.Sleep(method.methodComponent.Interval);
                    }
                    dataElements1 = new DataEntity <DataElement>();
                    foreach (DataColumn dc in columns)
                    {
                        if ((method.methodComponent.IsAsync || method.methodComponent.EnabledBuffer) &&
                            0 < method.methodComponent.Interval)
                        {
                            Thread.Sleep(method.methodComponent.Interval);
                        }
                        v = dr[dc.ColumnName];
                        dataElements1.Add(dc.ColumnName, v);
                    }
                    dataElements.Add(dataElements1);
                }

                v = dataElements;
            }
            else if (typeof(DataTable) == paraType)
            {
                v = dt;
            }
            else if (typeof(DataSet) == paraType)
            {
                v = new DataSet();
                ((DataSet)v).Tables.Add(dt);
            }
            else if (typeof(Collections.IEnumerable) == paraType.GetInterface("IEnumerable") && typeof(string) != paraType)
            {
                if (typeof(IList) == paraType.GetInterface("IList") || typeof(Array) == paraType.BaseType)
                {
                    if (paraType.BaseType == typeof(Array))
                    {
                        v = createArrayByType(paraType, dt.Rows.Count);
                        arrayAdd(method, v, dt);
                    }
                    else
                    {
                        v = createListByType(paraType);
                        listAdd(method, v, dt);
                    }
                }
                else
                {
                    //Dictionary
                }
            }
            else
            {
                v = Activator.CreateInstance(paraType);
                DataRowToEntity(method, dt.Rows[0], v);
            }
            if (null != v)
            {
                result = v;
            }
            return(result);
        }