2009-12-06 10 views
0

Lors de l'exécution de cette méthode:DataTable à la liste problème de conversion <T>

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

Je reçois cette exception:

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

Mais je sais que je l'ai déjà déclaré un constructeur dans ma classe 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() 
{ 
} 
+1

A quoi ressemble le constructeur de 'Northwind.BO.Products'? Le constructeur doit correspondre à 'args' à trouver. – dtb

Répondre

1

Vous n'avez pas besoin d'un constructeur sur une classe Northwind - selon l'exception, vous avez besoin du constructeur correct sur Northwind.BO.Products.

En outre, tel qu'utilisé par Activator.CreateInstance, ce constructeur doit avoir la bonne signature qui correspond à l'argument que vous lui passez. Puisque vous lui passez un argument, le constructeur par défaut ne correspondra pas.

Comme je l'ai lu votre question, il doit avoir ce constructeur

public Products(DataRow dr) 
+0

Le tableau d'objets fait partie de l'appel CreateInstance; CreateInstance va essayer de trouver un constructeur qui correspond au contenu de sorte que le tableau soit le meilleur. http://msdn.microsoft.com/en-us/library/wcxyzt4d.aspx – dtb

+0

Cela ne fonctionne pas non plus. public products (object [] args) {} – anonymous

+0

@dtb: Vous avez raison - ma faute. Edited ma réponse. –

2

Le problème est que la classe Northwind.BO.Products ne dispose pas d'un constructeur public qui prend un seul DataRow.

Pouvez-vous nous dire quels constructeurs il a?

+0

public Products() {} – anonymous

+0

Ensuite, vous pouvez seulement écrire 'Activator.CreateInstance (type)' sans aucun paramètre. – SLaks

0

La plus grande partie du constructeur ne correspond probablement pas à l'argument que vous fournissez.