Exemplo n.º 1
0
        private IScriptingHost GetHost()
        {
            string typeName;

            switch (language)
            {
            case "CSharp":
                typeName = "Purple.Scripting.CSharp.ScriptingHost";
                break;

            default:
                typeName = language;
                break;
            }
            return((IScriptingHost)TypeRegistry.Create(typeName));
        }
Exemplo n.º 2
0
        public static void CopyToEntity <T>(this DataRow row, T t)
        {
            //获取各自的属性
            TypeResolver r = TypeRegistry.Create <T>();
            PropertyInfo _p;
            object       rowData;
            int          enumValue;


            foreach (DataColumn c in row.Table.Columns)
            {
                _p      = r.FindPropertyByNameOrAlias(c.ColumnName);
                rowData = row[c.ColumnName];

                try
                {
                    if (_p != null && !(rowData is DBNull))
                    {
                        if (_p.PropertyType.IsEnum)
                        {
                            int.TryParse(rowData.ToString(), out enumValue);

                            if (enumValue != 0)
                            {
                                _p.SetValue(t, enumValue, null);
                            }
                            else
                            {
                                _p.SetValue(t, Enum.Parse(_p.PropertyType, rowData.ToString()), null);
                            }
                        }
                        else
                        {
                            _p.SetValue(t, Convert.ChangeType(rowData, _p.PropertyType), null);
                        }
                    }
                }
                catch
                {
                    continue;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///将当前DataTable中的行转换成实体集合(仅拷贝实体与数据表列名相同的数据)
        /// </summary>
        public static IList <T> ToEntityList <T>(this DataTable table) where T : new()
        {
            IList <T> list = new List <T>();
            T         t;
            object    rowData;
            //获取各自的属性
            TypeResolver r = TypeRegistry.Create <T>();
            PropertyInfo _p;


            int propertiesCount = -1,
            //对应的属性数量
                hasFoundPropertiesCount = 0; //已经赋值的数量


            foreach (DataRow dr in table.Rows)
            {
                t = new T();
                foreach (DataColumn c in table.Columns)
                {
                    _p = r.FindPropertyByNameOrAlias(c.ColumnName);
                    if (_p == null)
                    {
                        continue;
                    }

                    rowData = dr[c.ColumnName];
                    try
                    {
                        if (!(rowData is DBNull))
                        {
                            if (_p.PropertyType.IsEnum)
                            {
                                if (rowData.GetType() == typeof(Int32))
                                {
                                    _p.SetValue(t, Convert.ToInt32(rowData), null);
                                }
                                else
                                {
                                    _p.SetValue(t, Enum.Parse(_p.PropertyType, rowData.ToString()), null);
                                }
                            }
                            else
                            {
                                _p.SetValue(t, Convert.ChangeType(rowData, _p.PropertyType), null);
                            }
                        }
                    }
                    catch
                    {
                        continue;
                    }

                    //计算是否已经赋值完成
                    if (++hasFoundPropertiesCount == propertiesCount)
                    {
                        hasFoundPropertiesCount = 0;
                        break;
                    }
                }
                list.Add(t);


                //对所有属性赋值后跳出循环
                if (propertiesCount == -1)
                {
                    propertiesCount = hasFoundPropertiesCount;
                }
            }
            return(list);
        }
Exemplo n.º 4
0
        /// <summary>
        ///将DataReader转换成实体(仅拷贝实体与数据表列名相同的数据,DataReader游标已经指向实体数据对应位置)
        /// </summary>
        public static T CopyToEntity <T>(this DbDataReader reader, T t)
        {
            //获取表结构
            DataTable schemaTable = reader.GetSchemaTable();

            //表结构列名
            string columnName;

            //获取泛型的的所有属性
            TypeResolver r = TypeRegistry.Create <T>();
            PropertyInfo pro;
            object       rowData;


            int propertiesCount = r.PropertyLen();
            //属性数量
            int hasFoundPropertiesCount = 0;    //已经赋值的数量


            foreach (DataRow dr in schemaTable.Rows)
            {
                columnName = dr["ColumnName"].ToString();
                pro        = r.FindPropertyByNameOrAlias(columnName);
                if (pro == null)
                {
                    continue;
                }


                if (!((rowData = reader[columnName]) is DBNull))
                {
                    try
                    {
                        if (pro.PropertyType.IsEnum)
                        {
                            if (rowData.GetType() == typeof(Int32))
                            {
                                pro.SetValue(t, Convert.ToInt32(rowData), null);
                            }
                            else
                            {
                                pro.SetValue(t, Enum.Parse(pro.PropertyType, rowData.ToString()), null);
                            }
                        }
                        else
                        {
                            pro.SetValue(t, Convert.ChangeType(rowData, pro.PropertyType), null);
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }

                if (++hasFoundPropertiesCount == propertiesCount)
                {
                    hasFoundPropertiesCount = 0;
                    break;
                }
            }
            return(t);
        }
Exemplo n.º 5
0
        /// <summary>
        ///将DataReader中的行转换成实体集合(仅拷贝实体与数据表列名相同的数据)
        /// </summary>
        public static IList <T> ToEntityList <T>(this DbDataReader reader) where T : new()
        {
            IList <T> list = new List <T>();
            T         t;

            //获取表结构
            DataTable schemaTable = reader.GetSchemaTable();

            //表结构列名
            string columnName;


            //获取泛型的的所有属性
            TypeResolver r = TypeRegistry.Create <T>();
            PropertyInfo pro;
            object       rowData;

            int propertiesCount = -1,
            //对应的属性数量
                hasFoundPropertiesCount = 0; //已经赋值的数量

            while (reader.Read())
            {
                t = new T();

                foreach (DataRow dr in schemaTable.Rows)
                {
                    columnName = dr["ColumnName"].ToString();
                    pro        = r.FindPropertyByNameOrAlias(columnName);
                    if (pro == null)
                    {
                        continue;
                    }

                    //如果数据库中的值不为空,则赋值
                    if (!((rowData = reader[columnName]) is DBNull))
                    {
                        try
                        {
                            if (pro.PropertyType.IsEnum)
                            {
                                if (rowData.GetType() == typeof(Int32))
                                {
                                    pro.SetValue(t, Convert.ToInt32(rowData), null);
                                }
                                else
                                {
                                    pro.SetValue(t, Enum.Parse(pro.PropertyType, rowData.ToString()), null);
                                }
                            }
                            else
                            {
                                pro.SetValue(t, Convert.ChangeType(rowData, pro.PropertyType), null);
                            }
                        }

                        catch
                        {
                            continue;
                        }

                        //计算是否已经赋值完成
                        if (++hasFoundPropertiesCount == propertiesCount)
                        {
                            hasFoundPropertiesCount = 0;
                            break;
                        }
                    }
                }
                list.Add(t);


                //对所有属性赋值后跳出循环
                if (propertiesCount == -1)
                {
                    propertiesCount = hasFoundPropertiesCount;
                }
            }
            return(list);
        }