此方法有效,但它返回列表<object
>當我真正想要的是列表<Resource
>特定類型是資源時(和列表<YYY
>特定類型是YYY時,等等)。如何正確返回投射IEnumerable
如何重新排列方法簽名以返回列表<specificType
>?那麼,有沒有更好的方法來做到這一點?列表<object
>中的項目在本程序集中從許多不同類型反序列化。我正在嘗試創建ActualType列表並將該列表返回給調用者。希望這是有道理的。
private static ICollection<object> GetSpecificTypeList(Dictionary<string, List<object>> objectListDictionary, Type specificType)
{
Contract.Requires(objectListDictionary != null);
Contract.Requires(specificType != null);
var typeFullName = specificType.FullName;
var typedCollection = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(specificType));
var collection = objectListDictionary.SingleOrDefault(q => q.Key.Equals(typeFullName)).Value;
foreach (var obj in collection)
{
typedCollection.Add(Convert.ChangeType(obj, specificType));
}
return collection;
}
var resourceList = GetSpecificTypeList(package.FilesDictionary, typeof(Resource));
會動態幫助嗎?
爲什麼所有的寫照嗎?爲什麼不只是通用的開始呢? 'private static List GetSpecificTypeList (Dictionary > objectListDictionary){...}'? –