Cast d'une generic liste

Résolu
cs_blueangel Messages postés 69 Date d'inscription samedi 4 janvier 2003 Statut Membre Dernière intervention 19 mars 2009 - 4 juil. 2007 à 12:10
ricklekebekoi Messages postés 303 Date d'inscription mardi 11 février 2003 Statut Membre Dernière intervention 24 avril 2009 - 5 juil. 2007 à 01:48
bonjour
est ce que quelqu'un pourrait corriger ce bout de code?
public List<string> <string> GetAllBrands()
{
List<TblBrand> ListBrand;
//cette ligne de code provoque une erreur dans le code et le cast est incorrecte
ListBrand = (List<TblBrand>)Product.GetAllObjects("Brand");
......
}
public static List<object> GetAllObjects(string ObjectName)
{
...
}
merci !!!

blueangel13</string>

4 réponses

sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
4 juil. 2007 à 12:15
pourquoi ne pas faire :

public List<string> <string> GetAllBrands()
{
List<TblBrand> ListBrand;
//cette ligne de code provoque une erreur dans le code et le cast est incorrecte
ListBrand = Product.GetAllObjects<TblBrand>("Brand");
......
}
public static List<T> GetAllObjects<T>(string ObjectName)
{
...
}

Sébastien FERRAND (blog)
Consultant Indépendant
[Microsoft Visual C# MVP]
</string>
3
cs_blueangel Messages postés 69 Date d'inscription samedi 4 janvier 2003 Statut Membre Dernière intervention 19 mars 2009
4 juil. 2007 à 12:29
 une petite explication s'impose

blueangel13
0
cs_blueangel Messages postés 69 Date d'inscription samedi 4 janvier 2003 Statut Membre Dernière intervention 19 mars 2009
4 juil. 2007 à 12:37
comment récrire cette méthode?

public
static
List<
object> GetAllObjects(
string ObjectName){

IObjectScope scope =
ObjectScopeProvider1.ObjectScope();

string OQLQuery =
"Select * from " + ObjectName +
"Extent";

IQueryResult result = scope.GetOqlQuery(OQLQuery).Execute();

List<
object> entityCollection =
new
List<
object>();

foreach (
object entity
in result){

entityCollection.Add(entity);

}

return entityCollection;}

avec un cast correcte avec public static List<T> GetAllObjects<T>(string ObjectName)

blueangel13
0
ricklekebekoi Messages postés 303 Date d'inscription mardi 11 février 2003 Statut Membre Dernière intervention 24 avril 2009 5
5 juil. 2007 à 01:48
Je suis pas du tout certain de ce que j'avance, mais tentons le coup

publicstatic
List<T> GetAllObjects<T>(string ObjectName)


{










IObjectScope scope = ObjectScopeProvider1.ObjectScope();





string OQLQuery = "Select * from " + ObjectName + "Extent";





IQueryResult result = scope.GetOqlQuery(OQLQuery).Execute();





List<T> entityCollection = newList<T>();





foreach (T entity in result)


{




entityCollection.Add(entity);




}










return entityCollection;


}



avec un




Eric
0
Rejoignez-nous