Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

WPF C# Reflection: Iterate over all resources with build action "Page"

I have a .dll with a lot of ResourceDictionaries.

The build action of all these ResourceDictionaries is set to "Page".

Inside the Dll, I want to find all these ResourceDictionaries and iterate over them.

If I set the build action to "EmbeddedResource", I can use Reflection:

var embeddedResources = Assembly.GetExecutingAssembly().GetManifestResourceNames().ToList();

But GetManifestResourceNames() does not work for resources with build action "Page".

How do I find or iterate over all resources that have the build action "page"?

The solution doesn't have to be Reflection. Any other way is very welcome.

Thank you!

Solution:

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:

Enumerating .NET assembly resources at runtime

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();
        }
    }

If you call this method, you get all the resource strings with a ".baml" at the end and you can iterate over them.

Thank you Jon Wu for the right hint.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to this answer,

Page (WPF only): Used to compile a xaml file into baml. The baml is then embedded with the same technique as Resource (i.e. available as AppName.g.resources).

So it sounds like you just need to look for resources identified with YourAppName.g.resources.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...