-1

I have an UnmanagedMemoryStream in below code, How can I get the object from deserilizing it. I was trying to get a Resource (rd.xaml) from an Assembly :

string address = @"WpfControlLibrary1.dll";
Assembly skinAssembly = Assembly.LoadFrom(address);
string name = skinAssembly.GetName().Name +".g";
var manager = new ResourceManager(name, skinAssembly);
ResourceSet rs = manager.GetResourceSet(CultureInfo.CurrentCulture, true, true);

UnmanagedMemoryStream stream = (UnmanagedMemoryStream)rs.GetObject("rd.baml", true);

I'm not able to deserialize the content of Stream into a .net Object(which is a Resource Dictionary in above case). How can I do this?

PS: BinaryFormatter is throwing an exception while Deserialize operation.

Kylo Ren
  • 8,551
  • 6
  • 41
  • 66
  • What exception? In which line? – cassandrad Apr 17 '16 at 15:13
  • {"The input stream is not a valid binary format. The starting contents (in bytes) are: 0C-00-00-00-4D-00-53-00-42-00-41-00-4D-00-4C-00-00 ..."} – Kylo Ren Apr 17 '16 at 16:43
  • add these lines in my code: BinaryFormatter binForm = new BinaryFormatter(); Object obj = (Object)binForm.Deserialize(stream); – Kylo Ren Apr 17 '16 at 16:44
  • Did you serialized the resource dictionary with the same formatter that you use to deserialize? It looks like you are trying to do something strange. `rs.GetObject` already returns an Object according to MSDN. And then you are trying to assign it to a stream? And then deserialize? – cassandrad Apr 17 '16 at 16:55
  • Well, I didn't serialize resource it was as it is, all I'm doing is trying to get it from another assembly using reflection. and getObject returns UnmangedMemoryStream... use the ccode...see it yourself it doesn't return any other object. – Kylo Ren Apr 17 '16 at 17:30
  • [ResourceSet.GetObject(String, bool)](https://msdn.microsoft.com/en-us/library/0x0247w2(v=vs.110).aspx) returns Object. – cassandrad Apr 17 '16 at 21:03
  • @cassandradied And this case the object is UnmanagedMemoryStream . – Kylo Ren Apr 18 '16 at 05:22

1 Answers1

4

Below is how I've done it, with optimized code:

public ResourceDictionary GetResourceDictionary(string assemblyName)
    {
        Assembly asm = Assembly.LoadFrom(assemblyName);
        Stream stream = asm.GetManifestResourceStream(asm.GetName().Name + ".g.resources");            
        using (ResourceReader reader = new ResourceReader(stream))
        {
            foreach (DictionaryEntry entry in reader)
            {
                var readStream = entry.Value as Stream;
                Baml2006Reader bamlReader = new Baml2006Reader(readStream);
                var loadedObject = System.Windows.Markup.XamlReader.Load(bamlReader);
                if (loadedObject is ResourceDictionary)
                {
                    return loadedObject as ResourceDictionary;
                }
            }
        }
        return null;
    }

OUTPUT:

output

I wanted to read a ResourceDictionary from an External Assembly/Another Project, So that I can iterate it's Resources to use.

Kylo Ren
  • 8,551
  • 6
  • 41
  • 66