简体   繁体   English

要列出的数据表 <T> 转换问题

[英]DataTable to List<T> conversion problem

While executing this method: 执行此方法时:

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;
        }

I am getting this exception: 我收到此异常:

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

But I know that I already have declared a constructor in my Products class. 但是我知道我已经在我的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()
{
}

The problem is that the Northwind.BO.Products class doesn't have a public constructor that takes a single DataRow . 问题在于Northwind.BO.Products类没有采用单个DataRowpublic构造函数。

Can you tell us what constructors it has? 你能告诉我们它有什么构造函数吗?

You don't need a constructor on any Northwind class - according to the exception, you need the correct constructor on Northwind.BO.Products . 您不需要任何Northwind类的构造函数-根据异常,您需要Northwind.BO.Products上的正确构造函数。

Furthermore, as used by Activator.CreateInstance, this constructor must have the right signature that matches the argument you pass to it. 此外,如Activator.CreateInstance所使用的,此构造方法必须具有与传递给它的参数匹配的正确签名。 Since you are passing an argument to it, the default constructor will not match. 由于您要向其传递参数,因此默认构造函数将不匹配。

As I read your question, it must have this constructor 当我阅读您的问题时,它必须具有此构造函数

public Products(DataRow dr)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM