16

I've got an assembly somewhere on the file system, e.g. "C:\temp\test.dll". In that assembly there's a ResourceDictionary, e.g. "abc.xaml".

How can i get that ResourceDictionary? Maybe there is a way using Reflections? I didn't find a solution so far.

Thanks in advance!

Edit: Just wanted to add that I want to access the Resources in the Dictionary, e.g. a Style.

Christian Hubmann
  • 1,502
  • 3
  • 17
  • 21

3 Answers3

20

You actually need to write the Uri like this:

Assembly.LoadFrom(@"C:\temp\test.dll");
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri(@"pack://application:,,,/test;component/myresource.xaml");
  • 5
    Documentation for the goofy Uri syntax is here: http://msdn.microsoft.com/en-us/library/aa970069.aspx – scobi Feb 10 '10 at 22:59
9

Edit: I found an even better solution which works with ResourceDictionaries:

Assembly.LoadFrom(@"C:\temp\test.dll");
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri("/test;component/myresource.xaml");

Well, I couldn't get it to work with ResourceDictionaries, so I'm using good old Resource Files instead ;) For anyone interested, here is how I did it:

Assembly a = Assembly.LoadFile(@"C:\temp\test.dll");
ResourceManager rm = new ResourceManager("NameOfResource", a);
object o = rm.GetObject("xyz");

You can get "NameOfResource" with Reflector, as Ian suggested.

Christian Hubmann
  • 1,502
  • 3
  • 17
  • 21
2

Grab a copy of Reflector (Lutz has handed this over now). Use that to look at the assembly and the namespace etc of the resources in it.

Then read in the embedded resource something like this;

Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
using (System.IO.Stream s = asm.GetManifestResourceStream(<yourname>)
{
    using (System.IO.StreamReader reader = new System.IO.StreamReader(s))
    {
        string xml = reader.ReadToEnd();
    }
}
Dead account
  • 19,587
  • 13
  • 52
  • 82