代码之家  ›  专栏  ›  技术社区  ›  user366312

数据表到列表的转换问题

  •  0
  • user366312  · 技术社区  · 15 年前

    执行此方法时:

    public static List<T> ToList<T>(DataTable dataTable)
            {
                Type type = typeof(T);
    
                List<T> list = new List<T>();
    
                foreach (DataRow dr in dataTable.Rows)
                {
                    object[] args = new object[1];
    
                    args[0] = dr;
    
                    list.Add((T)Activator.CreateInstance(type, args));
                }
    
                return list;
            }
    

    我得到这个例外:

    Constructor on type 'Northwind.BO.Products' not found.
    

    但我知道我已经在 Products 班级。

    public class Products
    {
        private int _ProductID;
        [DataMapping("ProductID", -99)]
        public int ProductID
        {
            get { return _ProductID; }
            set
            {
                if (_ProductID <= 0)
                {
                    _ProductID = value;
                }
                else
                {
                    throw new Exception("ID should not be set manually!");
                }
            }
        }
    
        private string _ProductName;
        [DataMapping("ProductName", "")]
        public string ProductName
        {
            get { return _ProductName; }
            set { _ProductName = value;}
        }
    
        private int _SupplierID;
        [DataMapping("SupplierID", -99)]
        public int SupplierID
        {
            get { return _SupplierID; }
            set { _SupplierID = value;}
        }
    
        private int _CategoryID;
        [DataMapping("CategoryID", -99)]
        public int CategoryID
        {
            get { return _CategoryID; }
            set { _CategoryID = value;}
        }
    
        private string _QuantityPerUnit;
        [DataMapping("QuantityPerUnit", "")]
        public string QuantityPerUnit
        {
            get { return _QuantityPerUnit; }
            set { _QuantityPerUnit = value;}
        }
    
        private decimal _UnitPrice;
        [DataMapping("UnitPrice", -99.99)]
        public decimal UnitPrice
        {
            get { return _UnitPrice; }
            set { _UnitPrice = value;}
        }
    
        private short _UnitsInStock;
        [DataMapping("UnitsInStock", -99)]
        public short UnitsInStock
        {
            get { return _UnitsInStock; }
            set { _UnitsInStock = value;}
        }
    
        private short _UnitsOnOrder;
        [DataMapping("UnitsOnOrder", -99)]
        public short UnitsOnOrder
        {
            get { return _UnitsOnOrder; }
            set { _UnitsOnOrder = value;}
        }
    
        private short _ReorderLevel;
        [DataMapping("ReorderLevel", -99)]
        public short ReorderLevel
        {
            get { return _ReorderLevel; }
            set { _ReorderLevel = value;}
        }
    
        private bool _Discontinued;
        [DataMapping("Discontinued", false)]
        public bool Discontinued
        {
            get { return _Discontinued; }
            set { _Discontinued = value;}
        }
    
    public Products(object[] args)
    {
    }
    
    public Products()
    {
    }
    
    3 回复  |  直到 13 年前
        1
  •  1
  •   Mark Seemann    15 年前

    您不需要任何构造函数 Northwind 类-根据例外情况,您需要 对的 上的构造函数 Northwind.BO.Products .

    此外,正如Activator.CreateInstance使用的那样,此构造函数必须具有与传递给它的参数匹配的正确签名。由于要向其传递参数,因此默认构造函数将不匹配。

    当我读到你的问题时,它必须有这个构造函数

    public Products(DataRow dr)
    
        2
  •  2
  •   mskfisher KeithS    13 年前

    问题是 Northwind.BO.Products 班级没有 public 采用单个 DataRow .

    你能告诉我们它有什么构造器吗?

        3
  •  0
  •   Dave Markle    15 年前

    构造函数大多数签名可能与您提供的参数不匹配。