> >XLToList<IMyInterface> x; > >if(SomeCondition) > x = new XLToList<AClass>(); >else > x = new XLToList<ADifferentClass>(); > >>
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.....
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();