LinQ T tip listesindeki her giriş atan dökme yöntemi içeren System.Type ile
List<Object> obj = new List<Object>();
obj.Add("A");
obj.Add("B");
Bir çalışma dökme olabilir
var list = obj.Cast<string>();
Ben
Type t = typeof(String);
Object list = obj.Cast(t);
bir çözüm kullanmak olacaktır çalışmak istiyorum ne yansıma ve genel olarak bir liste oluşturmak ve doldurmak ama daha iyi bir çözüm var mı merak ediyorum? .NET 4.0'ın bunu yapmanın bir yolu olabilecek bazı eş/karşıt varyansı desteklemesi gerektiğini duydum.
Ekstra Bilgi ve Yansıma Soulution
aşağıdaki The model item passed into the dictionary is of type System.Collections.Generic.List1[IStatisticEntry], but this dictionary requires a model item of type System.Collections.Generic.List1[CrashStatistic+CrashEntry]
olduğunu olsun hatası. CrashEntry, IStaticEntry uygular, ancak liste genel bir türü olduğu için kullanılamaz.
public static object Cast(this IEnumerable list, Type type)
{
var newList = Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
foreach (var item in list)
newList.GetType().GetMethod("Add").Invoke(newList, new object[] { item });
return newList;
}
http://geekswithblogs.net/abhijeetp/archive/2010/01/10/covariance-and-contravariance-in-c-4.0.aspx –