У меня есть довольно простой метод:Обеспечение массив типов в качестве параметра метода
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;
}
Это работает отлично - я могу назвать метод и обеспечить тип, как Т. Тем не менее, то, что я хочу быть в состоянии сделать заключается в том, чтобы указать несколько типов. Поэтому я, ошибочно, предположил, что я мог бы реорганизовать метод к:
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)
.
Я знаю, что я делаю что-то неправильно, и я предполагаю, что мне нужно использовать Reflection здесь.
Замечание: вы можете использовать 'params' для последнего аргумента:', params Type [] types) ', чтобы иметь возможность вызывать его без преобразованных вручную элементов в массив. –