Loop through all Resources in ResourceManager - C#
Asked Answered
B

2

18

How do I loop into all the resources in the resourcemanager?

Ie: foreach (string resource in ResourceManager) //Do something with the recource.

Thanks

Budgerigar answered 26/9, 2008 at 14:58 Comment(0)
I
30

Use ResourceManager.GetResourceSet() for a list of all resources for a given culture. The returned ResourceSet implements IEnumerable (you can use 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:

using System.Linq;

...

var resourceSet = resourceManager.GetResourceSet(..);
var count = resSet.Cast<object>().Count();
Impassive answered 26/9, 2008 at 15:1 Comment(3)
how the hell do I get a count for it?Goodyear
@Nico: IEnumerable's don't implement a Count property but you can iterate the entries and count them. Alternatively you can cast the RessourceSet to IEnumerable<object> and use the Count() extension method which is new in C# 3.5: msdn.microsoft.com/en-us/library/…Impassive
Is there a way to loop through all available cultures? I am trying to write a T4 template that generates localization json files from resx files.Georgeanngeorgeanna
H
1

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

Hid answered 26/9, 2008 at 15:33 Comment(1)
We have a use for it where we allow a client admin to override language literals in the DB so we need a page to show all the resources they can override.Bergamot

© 2022 - 2024 — McMap. All rights reserved.