WPF C#反射:使用构建操作“页面"遍历所有资源. [英] WPF C# Reflection: Iterate over all resources with build action "Page"
问题描述
我有一个带有很多ResourceDictionaries的.dll.
I have a .dll with a lot of ResourceDictionaries.
所有这些ResourceDictionaries的构建操作都设置为页面" .
The build action of all these ResourceDictionaries is set to "Page".
在Dll内部,我想找到所有这些ResourceDictionaries并对其进行迭代.
Inside the Dll, I want to find all these ResourceDictionaries and iterate over them.
如果将构建操作设置为"EmbeddedResource",则可以使用Reflection:
If I set the build action to "EmbeddedResource", I can use Reflection:
var embeddedResources = Assembly.GetExecutingAssembly().GetManifestResourceNames().ToList();
但是GetManifestResourceNames()不能不适用于具有构建操作页面"的资源.
But GetManifestResourceNames() does not work for resources with build action "Page".
如何查找或遍历具有构建操作页面"的所有资源?
解决方案不必是反射.任何其他方式都非常欢迎.
谢谢!
解决方案:
女士们,先生们!我必须宣布,本周的得奖者和本次赏金得主是吴宗宪.乔恩·吴(Jon Wu)给出了正确的提示,并通过搜索找到了以下解决方案:
Ladies and Gentleman! I have to announce, that the man of the week and the winner of this bounty, is Mr. Jon Wu. Jon Wu gave the right hint and through searching, I found this solution:
稍作更改的工作代码如下:
The working code, slightly changed looks like this:
public static string[] GetResourceNames()
{
var asm = Assembly.GetExecutingAssembly();
string resName = asm.GetName().Name + ".g.resources";
using (var stream = asm.GetManifestResourceStream(resName))
using (var reader = new System.Resources.ResourceReader(stream))
{
return reader.Cast<DictionaryEntry>().Select(entry => (string)entry.Key).ToArray();
}
}
如果调用此方法,则所有资源字符串都以".baml"结尾,然后可以对其进行迭代.
If you call this method, you get all the resource strings with a ".baml" at the end and you can iterate over them.
感谢乔恩·吴(Jon Wu)的正确提示.
Thank you Jon Wu for the right hint.