У меня в сборке куча обычных, закрытых и открытых типов. У меня есть запрос, который я пытаюсь исключить из него открытые типы
class Foo { } // a regular type
class Bar<T, U> { } // an open type
class Moo : Bar<int, string> { } // a closed type
var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???);
types.Foreach(t => ConsoleWriteLine(t.Name)); // should *not* output "Bar`2"
После отладки общих аргументов открытого типа я обнаружил, что их FullName
равно нулю (а также другие вещи, такие как DeclaringMethod
). Таким образом, это может быть одним из способов:
bool IsOpenType(Type type)
{
if (!type.IsGenericType)
return false;
var args = type.GetGenericArguments();
return args[0].FullName == null;
}
Console.WriteLine(IsOpenType(typeof(Bar<,>))); // true
Console.WriteLine(IsOpenType(typeof(Bar<int, string>))); // false
Есть ли встроенный способ узнать, открыт ли тип? если нет, то есть ли лучший способ сделать это? Спасибо.
IsGenericType
?Use the ContainsGenericParameters property to determine whether a Type object represents an open constructed type or a closed constructed type.
- person Dark Falcon   schedule 12.09.2014ContainsGenericParameters
pop в intellisense, но я думал, что он возвращает true, если для типа есть какие-либо общие аргументы. Читая документ, кажется, что «аргумент» - это не то же самое, что «параметр»? @terrybozzio нет, наоборот, отфильтруйте их :) - person vexe   schedule 12.09.2014