数据表到列表转换问题

发布于 2024-08-13 05:29:09 字数 2806 浏览 2 评论 0原文

执行此方法时:

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()
{
}

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.

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()
{
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Bonjour°[大白 2024-08-20 05:29:09

问题在于 Northwind.BO.Products 类没有采用单个 DataRowpublic 构造函数。

你能告诉我们它有哪些构造函数吗?

The problem is that the Northwind.BO.Products class doesn't have a public constructor that takes a single DataRow.

Can you tell us what constructors it has?

皓月长歌 2024-08-20 05:29:09

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

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

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

public Products(DataRow dr)

You don't need a constructor on any Northwind class - according to the exception, you need the correct constructor on Northwind.BO.Products.

Furthermore, as used by Activator.CreateInstance, this constructor must have the right signature that matches the argument you pass to it. 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)
红颜悴 2024-08-20 05:29:09

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

The constructor most signature probably does not match the argument you're supplying.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文