Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Generic list question
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 5.0
OS:
Windows 7
Network:
SAMBA Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01611163
Message ID:
01611180
Vues:
50
>Hi Viv, what I need to be able to do is ( I don't know which class I'm going to need until runtime )
>
>
>
>XLToList<IMyInterface>  x;
>
>if(SomeCondition)
>    x = new XLToList<AClass>();
>else
>    x = new XLToList<ADifferentClass>();
>
>
>
>But I can't seem to figure it out :-( having a dumb day

I don't think you can dynamically assign the type. Assuming you have a class TDI1 that implements ITarriffDataImport something like this would work (but it's ugly):
Type generic = typeof(XlToList<>);

List<TDI1> thelist = new List<TDI1>();
thelist.Add(new TDI1());

Type[] typeArgs2 = { thelist[0].GetType() };

Type myXlToList = generic.MakeGenericType(typeArgs2);
dynamic xlTolistInstance =  Activator.CreateInstance(myXlToList);
 xlTolistInstance.Data = thelist;
Don't think there's a way to cast to a concrete type tho.....

Hmm. Last line doesn't work unless:
xlTolistInstance.Data = thelist.Cast<ITarriffDataImport>().ToList();
But at that point you may as well not use a generic class at all:
public class XlToList
    {
        public List<ITarriffDataImport> Data { get; set; } 
    }
and
XlToList xxx = new XlToList2();
xxx.Data = thelist.Cast<ITarriffDataImport>().ToList();
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform