我有一个资源程序集,其中包含使用构建的图像文件Resource or Content建立行动。这使得可以使用 Uris 访问这些文件。但是我找不到枚举此类资源的方法。
如果我将构建操作设置为嵌入式资源可以使用以下代码枚举文件:
string[] resources = Assembly.GetExecutingAssembly().GetManifestResourceNames();
但它反过来又使这些文件无法使用 Uris 访问。
问题是 - 如何枚举使用以下任一方法编译的资源Resource or Content建立行动?
注意:正如 Thomas Levesque 指出的那样,可以通过利用 AssemblyAssociatedContentFileAttribute 来枚举此类资源,但它似乎仅适用于 WPF 应用程序程序集,而不适用于类库程序集。所以这个问题仍然悬而未决。
您可以枚举AssemblyAssociatedContentFile http://msdn.microsoft.com/en-us/library/system.windows.resources.assemblyassociatedcontentfileattribute.aspx程序集上定义的属性:
var resourceUris = Assembly.GetEntryAssembly()
.GetCustomAttributes(typeof(AssemblyAssociatedContentFileAttribute), true)
.Cast<AssemblyAssociatedContentFileAttribute>()
.Select(attr => new Uri(attr.RelativeContentFilePath));
您还可以检查这一页 http://mostlytech.blogspot.com/2007/09/enumerating-xaml-baml-files-in-assembly.html了解枚举 BAML 资源的方法。
更新:实际上上面的解决方案仅适用于内容文件。下面的方法返回所有资源名称(包括BAML资源、图像等):
public static string[] GetResourceNames()
{
var asm = Assembly.GetEntryAssembly();
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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)