0
我有一個相當簡單的方法:提供了類型數組作爲方法的參數
public static LinkItemCollection ToList<T>(this LinkItemCollection linkItemCollection)
{
var liCollection = linkItemCollection.ToList(true);
var newCollection = new LinkItemCollection();
foreach (var linkItem in liCollection)
{
var contentReference = linkItem.ToContentReference();
if (contentReference == null || contentReference == ContentReference.EmptyReference)
continue;
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
IContentData content = null;
var epiObject = contentLoader.TryGet(contentReference, out content);
if (content is T)
newCollection.Add(linkItem);
}
return newCollection;
}
這工作得很好 - 我可以調用該方法,並提供一個類型T.不過,我希望能夠做到是能夠指定多種類型的。因此,我錯誤地認爲我可以重構方法:
public static LinkItemCollection ToList(this LinkItemCollection linkItemCollection, Type[] types)
{
var liCollection = linkItemCollection.ToList(true);
var newCollection = new LinkItemCollection();
foreach (var linkItem in liCollection)
{
var contentReference = linkItem.ToContentReference();
if (contentReference == null || contentReference == ContentReference.EmptyReference)
continue;
foreach (var type in types)
{
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
IContentData content = null;
var epiObject = contentLoader.TryGet(contentReference, out content);
if (content is type)
newCollection.Add(linkItem);
}
}
return newCollection;
}
但是,Visual Studio是顯示它無法解決就行if(content is type)
類型的符號。
我知道我做錯了什麼,我猜我需要在這裏使用反射。
旁註:你可能希望使用'params'的最後一個參數:',則params類型[]類型)'要能夠把它無需手動轉換項目陣列。 – 2014-10-31 01:54:12