简体   繁体   English

如何遍历ProcessThreadCollection以访问集合中每个对象的属性?

[英]How can I iterate over a ProcessThreadCollection to access a property of each object inside the collection?

I have a simple loop that attempts to iterate over a ProcessThreadCollection variable 我有一个简单的循环,尝试对ProcessThreadCollection变量进行迭代

Inside the loop, I am trying to access a property of each object in the collection like so 在循环内部,我试图像这样访问集合中每个对象的属性

foreach (var thread in Process.GetProcessesByName(processName)[0].Threads)
{
    var threadId = thread.Id;   

    Console.WriteLine(threadId);   
}

However, I get an error stating the following 但是,出现以下错误提示

Cannot resolve symbol 'Id' 无法解析符号“ Id”

If I do the following, I can access the Id property of an object within the collection 如果执行以下操作,则可以访问集合中对象的Id属性

var threadCollection = Process.GetProcessesByName(processName)[0].Threads;

var threadId = threadCollection[0].Id;

Console.WriteLine(threadId);

How could I change my loop so that I could access the Id property of each object in the ProcessThreadCollection ? 如何更改循环,以便可以访问ProcessThreadCollection中每个对象的Id属性?

Process.Threads property returns ProcessThreadCollection which does not implement generic IEnumerable<T> . Process.Threads属性返回不实现通用IEnumerable<T> ProcessThreadCollection Instead, it derives from ReadOnlyCollectionBase , which in turn implements non-generic ICollection and IEnumerable . 相反,它是从ReadOnlyCollectionBase派生的,后者又实现了非通用的ICollectionIEnumerable

Because of all that, when you defined your loop using var the type of the loop variable thread is inferred to be object . 因此,在使用var定义循环时,将循环变量thread的类型推断为object

You can work around that in two ways: 您可以通过两种方法解决此问题:

Either explicitly specify the type: 明确指定类型:

foreach (ProcessThread thread in Process.GetProcessesByName(processName)[0].Threads)

or call Cast<ProcessThread> : 或致电Cast<ProcessThread>

foreach (var thread in Process.GetProcessesByName(processName)[0].Threads.Cast<ProcessThread>())

Both of these will make the compiler realize thread is of type ProcessThread and not object and will give you access to all the properties of that type, including Id . 这两种方法都会使编译器意识到thread属于ProcessThread类型而不是object类型,并使您能够访问该类型的所有属性,包括Id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何迭代集合并使用LINQ扩展方法更改值? - How can I iterate over a collection and change values with LINQ extension methods? 在C#中,如何按对象中的属性对集合进行排序? - In C#, How can I sort a collection by a property in an object? 如何将可编写脚本的 object 的每个实例添加到集合中? - How can I add each instance of a scriptable object to a collection? 迭代收集,并使用OR运算符将其分配给单个属性 - Iterate Collection and Assign Each to Single Property with OR Operator 如何检查具有特定属性的对象的List集合? - How can I check a List collection for an object that has a particular property? 当我只能访问列表的对象并且不知道type参数时,如何遍历列表的项目? - How to iterate over items of a list when I only have access to the object of that list and dont know the type parameter? 将属性从模型复制回对象-显式复制还是遍历属性集合? - Copying properties from a model back to an object — copy explicitly or iterate over property collection? 如何更改作为对象一部分的集合内的字段的值 - How can I change the value of a fields inside a collection that is part of an object 我如何遍历Excel文件 - How can I iterate over an excel file 如何在对象集合和每个对象内部的集合上运行LINQ查询 - How to run a LINQ query on a collection of objects and a collection inside of each object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM