Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ使用具有给定类型的Type()来获取类_C#_Linq - Fatal编程技术网

C# LINQ使用具有给定类型的Type()来获取类

C# LINQ使用具有给定类型的Type()来获取类,c#,linq,C#,Linq,我想归档一个列表,类型为IA(一个接口)。假设我有以下结构: interface IA { // Some definitions here... } class B : IA {} class C : IA {} 我想使用LINQ过滤列表中的B和C。通常,我会使用myList.OfType().ToList()方法,但由于我不知道在我的方法中,我要筛选什么类型,我需要找到我正在使用的列表的基本类型: Type[] types = myList.GetType().GetGeneric

我想归档一个
列表
,类型为
IA
(一个接口)。假设我有以下结构:

interface IA {
    // Some definitions here...
}
class B : IA {}
class C : IA {}
我想使用LINQ过滤列表中的
B
C
。通常,我会使用
myList.OfType().ToList()
方法,但由于我不知道在我的方法中,我要筛选什么类型,我需要找到我正在使用的列表的基本类型:

Type[] types = myList.GetType().GetGenericArguments();
// type[0] == typeof(B)
现在我想做一些类似的事情:

List<IA> filteredList = myList.OfType<type[0]>().ToList();
List filteredList=myList.OfType().ToList();
这不起作用,原因很明显,
OfType()
只接受类而不接受类型。我的问题是:我如何从一个类型回到一个类

编辑:将我想要获得的结果从
列表
更改为
列表

Edit2:一个有效的示例

class Program
{
    static List<IExample> exampleList;

    static void Main(string[] args)
    {
        exampleList = new List<IExample>();

        A a = new A();
        B b = new B();

        exampleList.Add(a);
        exampleList.Add(b);

        List<IExample> anotherList = new List<IExample>();
        anotherList.Add(new A());

        FilterList(anotherList);
    }

    static void FilterList(List<IExample> anotherList)
    {
        Type t = anotherList.ElementAt(0).GetType();

        // This does not work:
        //exampleList.OfType<t>().ToList();

        // This does work:
        List<IExample> filteredList = exampleList.Where(item => item.GetType() == t).ToList();

        // The filtered List is then used for further processing ...
    }
}


interface IExample {}
class A : IExample {}
class B : IExample {}
类程序
{
静态列表示例列表;
静态void Main(字符串[]参数)
{
exampleList=新列表();
A=新的A();
B=新的B();
示例列表。添加(a);
示例列表。添加(b);
List anotherList=新列表();
添加(新的A());
过滤器列表(另一个列表);
}
静态空过滤器列表(另一个列表)
{
Type t=anotherList.ElementAt(0.GetType();
//这不起作用:
//exampleList.OfType().ToList();
//这确实有效:
List filteredList=exampleList.Where(item=>item.GetType()==t.ToList();
//过滤后的列表将用于进一步处理。。。
}
}
接口示例{}
A类:IExample{}
B类:IExample{}

以下片段似乎回答了最初的问题:

interface IA {}
class B : IA {}
class C : IA {}

var myList = new List<IA>{new B(), new C()};
var myType = myList[0].GetType();
var myFilteredList = myList.Where(elt => elt.GetType().Equals(myType)).ToList<IA>(); 
接口IA{}
B类:IA{}
C类:IA{}
var myList=新列表{new B(),new C()};
var myType=myList[0].GetType();
var myFilteredList=myList.Where(elt=>elt.GetType().Equals(myType)).ToList();

尝试将
Type.IsAssignableFrom
Where
-扩展方法结合使用。出于好奇,您是否有这样一个示例,说明为什么希望有这样一个类型化的列表(“code>列表””)?有没有理由不让您的方法在您要筛选的类型中成为泛型的?还请注意,您的术语相当混乱-它不是关于类型与类,而是关于类型与表达式。。。一个类型参数必须是一个类型或另一个类型参数,而不是类型为
type
的表达式。只需执行“Where(c=>c是IA)。选择(c=>(IA)c)。ToList()”@Caramiriel我明白了,这是不必要的,并编辑了这个问题。我很困惑;在编译时不知道元素类型的情况下,如何声明变量
myList
?一个有趣的问题是如何处理case
classd:B{}。。。{new B(),new C(),new D()}…
即使
D
B
的一个实例,您的代码也不会在列表中包含
D
。Eric Lippert,您说得很对。当直接处理对象类型时,似乎总是有一个怪癖,特别是当存在继承时。