我有一个包含int和Resource类型的对象的字典。 Resource对象包含一个名为CreateNode()的方法。 如果我写这样的foreach循环:
foreach (var resourcePair in ResourceDictionary) { // call CreateNode() on each Resource object in dictionary pair }如何在字典中的每个资源对象上调用CreateNode()方法? 我试过以下,但编辑不喜欢它(说“无法解析符号”)。 这是正确的,因为它看起来我试图调用该对上的CreateNode方法,而不是该对的一部分对象上的CreateNode()方法。
foreach (var resourcePair in ResourceDictionary) { resourcePair.CreateNode(ref xElement); }我无法弄清楚如何做到这一点。 有人可以向正确的方向提供指针吗?
I have a dictionary that contains an int and an object of type Resource. The Resource object contains a method called CreateNode(). If I write a foreach loop like this:
foreach (var resourcePair in ResourceDictionary) { // call CreateNode() on each Resource object in dictionary pair }How can I call the CreateNode() method on each Resource object in the dictionary? I've tried the following, but the editor doesn't like it (says "Cannot resolve symbol"). Which is correct since it appears I'm trying to call the CreateNode method on the pair instead of the CreateNode() method on the object that is part of the pair.
foreach (var resourcePair in ResourceDictionary) { resourcePair.CreateNode(ref xElement); }I just can't figure out how to do this. Can someone offer a pointer in the right direction?
最满意答案
您需要执行以下操作:
foreach (var resourcePair in ResourceDictionary.Values) { resourcePair.CreateNode(ref xElement); }你需要指定你想访问的值(这是你的资源对象集合),否则你只是访问一个没有CreateNode方法的KeyValuePair。
You need to do the following:
foreach (var resourcePair in ResourceDictionary.Values) { resourcePair.CreateNode(ref xElement); }You need to specify that you want to access the values (which is your collection of resource objects) otherwise you are just accessing a KeyValuePair that doesnt have a CreateNode method.
更多推荐
发布评论