检查通用参数,是否在[]类型中?

时间:2016-03-24 16:00:54

标签: c# generics

我有这样的事情:

public byte[] resultA<T>(string some, T myCustomGenericParameter) 
{
    Type[] typList = GetAllTypesFromAssembly(Assembly.LoadFile("aaaa.dddd.dll"));

    // checking wether my custom parameter is in "typList"
    typList.OfType<myCustomGenericParameter>().Any()
}

我想检查我的通用参数是否在typList,但我得到Error 1 The type or namespace name 'myCustomGenericParameter' could not be found (are you missing a using directive or an assembly reference?)

1 个答案:

答案 0 :(得分:3)

  

我想检查我的通用参数是否在typList

然后使用

typeList.Contains(typeof(T));

typeList.Contains(myCustomGenericParameter.GetType());

如果myCustomGenericParameter可能是T的子类型,并且您想要查找该特定类型。

请注意typList.OfType<T>在这里不是很有用,因为集合中的所有对象都是Type个对象,因此OfType并不能真正区分它们。 OfType在应用于具有不同类型对象的集合时有效。

相关问题