APCGC_CBS.Common/DSToModel.cs

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace APCGC_CBS.Common
{
    public class DSToModel<T> where T : new()
    {
        /// <summary>
        /// 将DataSet轉化為List
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static List<T> ConvertToList(DataSet ds)
        {
            // 定義集合
            List<T> ts = new List<T>();
            try
            {
                // 獲得此模型的類型
                Type type = typeof(T);
                string tempName = "";
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataTable dt = ds.Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        T t = new T();
                        // 獲得此模型的公用變量
                        PropertyInfo[] propertys = t.GetType().GetProperties();

                        foreach (PropertyInfo proper in propertys)
                        {
                            tempName = proper.Name.ToUpper();

                            // 檢查DataTable是否包含此列
                            if (dt.Columns.Contains(tempName))
                            {
                                object value = dr[tempName];
                                if (value != DBNull.Value)
                                {
                                    if (proper.PropertyType == typeof(bool) && value.ToString() == "1")
                                        proper.SetValue(t, value, null);
                                    else if (proper.PropertyType == typeof(bool) && value.ToString() != "1")
                                        proper.SetValue(t, value, null);
                                    else
                                    {
                                        if (proper.PropertyType.ToString().Contains("System.Nullable"))
                                        {
                                            string propertytype = proper.PropertyType.ToString().Substring(proper.PropertyType.ToString().IndexOf('[') + 1);
                                            propertytype = propertytype.Remove(propertytype.LastIndexOf(']'));
                                            proper.SetValue(t, Convert.ChangeType(value, TypeDelegator.GetType(propertytype)), null);
                                        }
                                        else
                                            proper.SetValue(t, Convert.ChangeType(value, proper.PropertyType), null);
                                    }
后面还有 67 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。