.resx 파일의 모든 리소스를 반복
.resxC #에서 파일의 모든 리소스를 반복하는 방법이 있습니까?
항상 세계화를 고려하기 위해 항상 자원 관리자를 사용하고 파일을 직접 읽지합니다.
using System.Collections;
using System.Globalization;
using System.Resources;
...
ResourceManager MyResourceClass = new ResourceManager(typeof(Resources /* Reference to your resources class -- may be named differently in your case */));
ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
{
string resourceKey = entry.Key.ToString();
object resource = entry.Value;
}
내 블로그에서 알다시피 블로그 :) 짧은 버전은 자원의 전체 이름을 찾는 것입니다.
var assembly = Assembly.GetExecutingAssembly();
foreach (var resourceName in assembly.GetManifestResourceNames())
System.Console.WriteLine(resourceName);
사례를 위해 위험을 모두 사용하는 경우 :
foreach (var resourceName in assembly.GetManifestResourceNames())
{
using(var stream = assembly.GetManifestResourceStream(resourceName))
{
// Do something with stream
}
}
실행중인 어셈블리가 아닌 다른 어셈블리에서 리소스를 사용하는 Assembly클래스 의 다른 정적 메서드 중 일부를 사용하여 다른 어셈블리 개체를 사용 합니다. 그것이 도움이되기를 바랍니다 :)
ResXResourceReader rsxr = new ResXResourceReader("your resource file path");
// Iterate through the resources and display the contents to the console.
foreach (DictionaryEntry d in rsxr)
{
Console.WriteLine(d.Key.ToString() + ":\t" + d.Value.ToString());
}
// Create a ResXResourceReader for the file items.resx.
ResXResourceReader rsxr = new ResXResourceReader("items.resx");
// Create an IDictionaryEnumerator to iterate through the resources.
IDictionaryEnumerator id = rsxr.GetEnumerator();
// Iterate through the resources and display the contents to the console.
foreach (DictionaryEntry d in rsxr)
{
Console.WriteLine(d.Key.ToString() + ":\t" + d.Value.ToString());
}
//Close the reader.
rsxr.Close();
링크 참조 : Microsoft 예제
리소스 .RESX 파일을에 추가하는 순간 Visual Studio는 동일한 이름의 Designer.cs를 만들어 리소스의 모든 항목을 정적 속성으로 포함하는 클래스를 만듭니다. 리소스 파일의 이름을 입력 한 후 편집기에 점을 입력하면 리소스의 모든 이름을 볼 수 있습니다.
또는 리플렉션을 사용하여 기존 이름을 반복 할 수 있습니다.
Type resourceType = Type.GetType("AssemblyName.Resource1");
PropertyInfo[] resourceProps = resourceType.GetProperties(
BindingFlags.NonPublic |
BindingFlags.Static |
BindingFlags.GetProperty);
foreach (PropertyInfo info in resourceProps)
{
string name = info.Name;
object value = info.GetValue(null, null); // object can be an image, a string whatever
// do something with name and value
}
이 방법은 RESX 파일이 현재 어셈블리 또는 프로젝트의 범위에있을 때만 사용할 수 있습니다. 복수의 "pulse"에서 제공하는 방법을 사용하십시오.
이 방법의 장점은 원하는 경우 현지화를 고려하여 실제 속성을 호출한다는 것입니다. 그러나 일반적으로 리소스 속성을 호출하는 형식 안전 직접 메서드를 약간 사용합니다.
ResourceManager.GetResourceSet 을 사용할 수 있습니다 .
LINQ를 사용하는 비용 resourceSet.OfType<DictionaryEntry>()입니다. 예를 들어 LINQ를 사용하면 키 (문자열) 대신 보안 정책 (int)을 기준으로 리소스를 선택할 수 있습니다.
ResourceSet resourceSet = Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (var entry in resourceSet.OfType<DictionaryEntry>().Select((item, i) => new { Index = i, Key = item.Key, Value = item.Value }))
{
Console.WriteLine(@"[{0}] {1}", entry.Index, entry.Key);
}
너겟 패키지 System.Resources.ResourceManager(V4.3.0)에서는 ResourceSet및 ResourceManager.GetResourceSet사용할 수 없습니다.
ResourceReader이 게시물에서 제안한대로를 사용하여 " C # -ResourceManager (위성 어셈블리에서) 에서 제안한대로를 사용하여 없습니다. "
여전히 리소스 파일의 키 / 값을 읽을 수 있습니다.
System.Reflection.Assembly resourceAssembly = System.Reflection.Assembly.Load(new System.Reflection.AssemblyName("YourAssemblyName"));
String[] manifests = resourceAssembly.GetManifestResourceNames();
using (ResourceReader reader = new ResourceReader(resourceAssembly.GetManifestResourceStream(manifests[0])))
{
System.Collections.IDictionaryEnumerator dict = reader.GetEnumerator();
while (dict.MoveNext())
{
String key = dict.Key as String;
String value = dict.Value as String;
}
}
사용 SQL에 LINQ를 :
XDocument
.Load(resxFileName)
.Descendants()
.Where(_ => _.Name == "data")
.Select(_ => $"{ _.Attributes().First(a => a.Name == "name").Value} - {_.Value}");
참고 URL : https://stackoverflow.com/questions/2041000/loop-through-all-the-resources-in-a-resx-file
'IT' 카테고리의 다른 글
| 범위에서 임의의 이중 생성 (0) | 2020.07.11 |
|---|---|
| UnicodeDecodeError : 'utf8'코덱이 위치 0에서 바이트 0xa5를 사용할 수 없습니다 : 유효하지 않은 시작 바이트 (0) | 2020.07.10 |
| iPad 브라우저 너비 및 높이 표준 (0) | 2020.07.10 |
| Visual Studio에서 조건부 중단 점을 설정하는 방법은 무엇입니까? (0) | 2020.07.10 |
| 어디에서 어떻게 YAML 매핑을 OrderedDicts로로드 할 수 있습니까? (0) | 2020.07.10 |