循环遍历 ResourceManager 中的所有资源 - C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/140043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop through all Resources in ResourceManager - C#
提问by vIceBerg
How do I loop into all the resources in the resourcemanager?
如何循环访问资源管理器中的所有资源?
Ie: foreach (string resource in ResourceManager) //Do something with the recource.
即: foreach (ResourceManager 中的字符串资源) //用资源做点什么。
Thanks
谢谢
采纳答案by VVS
Use ResourceManager.GetResourceSet() for a list of all resources for a given culture. The returned ResourceSet implements IEnumerable (you can use foreach).
使用资源管理器。GetResourceSet() 获取给定文化的所有资源列表。返回的 ResourceSet 实现了 IEnumerable(您可以使用 foreach)。
To answer Nico's question: you can count the elements of an IEnumerable
by casting it to the generic IEnumerable<object>
and use the Enumerable.Count<T>()
extension method, which is new in C# 3.5:
回答 Nico 的问题:您可以IEnumerable
通过将an转换为泛型IEnumerable<object>
并使用Enumerable.Count<T>()
扩展方法来计算 an 的元素,这是 C# 3.5 中的新方法:
using System.Linq;
...
var resourceSet = resourceManager.GetResourceSet(..);
var count = resSet.Cast<object>().Count();
回答by Leandro López
I wonder why would you like to loop through all of the resources.
我想知道您为什么要遍历所有资源。
Anyway, ResourceManager
needs to be instantiated giving it a Type
or the base name where to lookup for resources. Then you will be able to retrieve a ResourceSet
but for a given CultureInfo
, ergo if you want to obtain all the resources for a given `ResourceManager
无论如何,ResourceManager
需要实例化给它一个Type
或查找资源的基本名称。然后,如果您想获取给定`ResourceManager 的所有资源,您将能够检索ResourceSet
给定的但是CultureInfo