PiotrK
PiotrK

Reputation: 4453

How to access default resource from other assembly

I have default resource file (*.resx) compiled in other assembly (just as default Properties.Resources), this assembly referenced by Assembly object. How can I access specific resource object inside it?

I tried:

public static class Resource
{
    public static T Get<T>(Assembly assembly, string name)
    {
        ResourceManager manager = new ResourceManager(assembly.GetName().Name + ".Properties.Resources", assembly);
        return (T)manager.GetObject(name);
    }
}

This throws MissingManifestResourceException and debugger shows that through manager is initialized as ResourceManager instance, the manager.ResourceSet is empty.

Upvotes: 0

Views: 178

Answers (1)

Ponas Justas
Ponas Justas

Reputation: 305

So the assembly itself does not know about the default namespace. However types know what is the namespace they belong to. Try this: new System.Resources.ResourceManager(assembly.DefinedTypes.First(x => x.Name == "Resources").FullName, assembly); Of course, ideally you should make more checks to make it fool proof, but this should put you on a correct track

Upvotes: 1

Related Questions