2

I want to load any resource from any assembly (of the same app) using only its uri. Both assemblies are part of the same application.

Because I want to be able to load from any assembly, I can't use "App" or "Application" which is not defined in a general Dll.

I think part or the answer is using : System.Reflection.Assembly.GetCallingAssembly().GetManifestResourceStream(path);

But I can't find how to properly find/extract/resolve the assembly from the uri?

Notes:

  • The resource is actually defined as "Resource".
  • The resource is not par of a WPF resource dictionary.
  • The resource is actually an XML file but could be anything

Up to now (~3 hours after), after using reflector, it seems that using static System.Windows.Application.GetResourceStream(uri).Stream has advantages like resource caching. It is bad because it is hooked to WPF (System.windows). I'm looking for a better way (non depending on any specific UI framework) to do the job.

Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
  • Are the assemblies are embedded in the current calling assembly? If they aren't,[`GetManifestResourceStream`](https://msdn.microsoft.com/en-us/library/xc4235zt(v=vs.110).aspx) wouldn't help here. Also, have you read [this](http://stackoverflow.com/questions/1137781/c-sharp-correct-way-to-load-assembly-find-class-and-call-run-method)? – Yuval Itzchakov Sep 28 '15 at 15:59
  • http://stackoverflow.com/questions/1477843/difference-between-loadfile-and-loadfrom-with-net-assemblies ... `LoadFrom` support Uri... – Alexei Levenkov Sep 28 '15 at 16:01
  • @AlexeiLevenkov, I want to load resource, not an assembly. – Eric Ouellet Sep 28 '15 at 16:03
  • @EricOuellet but you already posted code to load resource from assembly... It sounds like you just need to know how to load assembly from Uri... I may have misunderstood problem you have. – Alexei Levenkov Sep 28 '15 at 16:05
  • @YuvalItzchakov, Yes. Both assembly are part of the same app. I corrected my question. – Eric Ouellet Sep 28 '15 at 16:07
  • @AlexeiLevenkov, I only say that I think that using GetManifestResourceStream is part of the solution but I'm not sure. I don't know how to go further. Perhaps there is a better way without using it? – Eric Ouellet Sep 28 '15 at 16:09
  • Is this a WPF resource dictionary URI? Can you give an example of the URI you are wanting to load? – Ron Beyer Sep 28 '15 at 16:10
  • @RonBeyer, The resource I want to load is an XML file. No it is not part of a WPF resource dictionary. – Eric Ouellet Sep 28 '15 at 16:14

1 Answers1

8

I solved my problem with this "LoadResourceFromUri" fonction defined below. I've done and published a project at GitHub with a tool to load any module (dll) and display its resources. Could also get info on uri.

About the offset of 4 while converting byteArray to stream, I used Microsoft code because otherwise (without offset), it just don't work as expected.

enter image description here

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Resources;

namespace HQ.Util.General
{
    public static class ResourceHelper
    {
        // ******************************************************************
        /// <summary>
        /// The resource should be defined as 'Resource' not as 'Embedded resource'.
        /// </summary>
        /// <param name="resourcePath">The resource path</param>
        /// <param name="assembly">If null, then use calling assembly to find the resource</param>
        /// <returns></returns>
        public static Uri GetLocationUri(string resourcePath, Assembly assembly = null)
        {
            if (assembly == null)
            {
                assembly = Assembly.GetCallingAssembly();
            }

            resourcePath = resourcePath.Replace('\\', '/');

            return new Uri(@"pack://application:,,,/" + assembly.GetName().Name + ";component" +
                (resourcePath[0] == '/' ? resourcePath : "/" + resourcePath), UriKind.Absolute);
        }

        // ******************************************************************
        /// <summary>
        /// Will load resource from any assembly that is part of the application.
        /// It does not rely on Application which is specific to a (UI) frameowrk.
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="asm"></param>
        /// <returns></returns>
        public static Stream LoadResourceFromUri(Uri uri, Assembly asm = null)
        {
            Stream stream = null;

            if (uri.Authority.StartsWith("application") && uri.Scheme == "pack")
            {
                string localPath = uri.GetComponents(UriComponents.Path, UriFormat.UriEscaped);

                int indexLocalPathWithoutAssembly = localPath.IndexOf(";component/");
                if (indexLocalPathWithoutAssembly == -1)
                {
                    indexLocalPathWithoutAssembly = 0;
                }
                else
                {
                    indexLocalPathWithoutAssembly += 11;
                }

                if (asm != null) // Take the provided assembly, do not check for the asm in the uri.
                {
                    stream = GetAssemblyResourceStream(asm, localPath.Substring(indexLocalPathWithoutAssembly));
                }
                else
                {
                    if (uri.Segments.Length > 1)
                    {
                        if (uri.Segments[0] == "/" && uri.Segments[1].EndsWith(";component/"))
                        {
                            int index = uri.Segments[1].IndexOf(";");
                            if (index > 0)
                            {
                                string assemblyName = uri.Segments[1].Substring(0, index);

                                foreach (Assembly asmIter in AppDomain.CurrentDomain.GetAssemblies())
                                {
                                    if (asmIter.GetName().Name == assemblyName)
                                    {
                                        stream = GetAssemblyResourceStream(asmIter, localPath.Substring(indexLocalPathWithoutAssembly));
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (stream == null)
                    {
                        asm = Assembly.GetCallingAssembly();
                        stream = GetAssemblyResourceStream(asm, localPath.Substring(indexLocalPathWithoutAssembly));
                    }
                }
            }
            return stream;
        }

        // ******************************************************************
        /// <summary>
        /// The path separator is '/'.  The path should not start with '/'.
        /// </summary>
        /// <param name="asm"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Stream GetAssemblyResourceStream(Assembly asm, string path)
        {
            // Just to be sure
            if (path[0] == '/')
            {
                path = path.Substring(1);
            }

            // Just to be sure
            if (path.IndexOf('\\') == -1)
            {
                path = path.Replace('\\', '/');
            }

            Stream resStream = null;

            string resName = asm.GetName().Name + ".g.resources"; // Ref: Thomas Levesque Answer at:
            // http://stackoverflow.com/questions/2517407/enumerating-net-assembly-resources-at-runtime

            using (var stream = asm.GetManifestResourceStream(resName))
            {
                using (var resReader = new System.Resources.ResourceReader(stream))
                {
                    string dataType = null;
                    byte[] data = null;
                    try
                    {
                        resReader.GetResourceData(path.ToLower(), out dataType, out data);
                    }
                    catch (Exception ex)
                    {
                        DebugPrintResources(resReader);
                    }

                    if (data != null)
                    {
                        switch (dataType) // COde from 
                        {
                            // Handle internally serialized string data (ResourceTypeCode members).
                            case "ResourceTypeCode.String":
                                BinaryReader reader = new BinaryReader(new MemoryStream(data));
                                string binData = reader.ReadString();
                                Console.WriteLine("   Recreated Value: {0}", binData);
                                break;
                            case "ResourceTypeCode.Int32":
                                Console.WriteLine("   Recreated Value: {0}", BitConverter.ToInt32(data, 0));
                                break;
                            case "ResourceTypeCode.Boolean":
                                Console.WriteLine("   Recreated Value: {0}", BitConverter.ToBoolean(data, 0));
                                break;
                            // .jpeg image stored as a stream.
                            case "ResourceTypeCode.Stream":
                                ////const int OFFSET = 4;
                                ////int size = BitConverter.ToInt32(data, 0);
                                ////Bitmap value1 = new Bitmap(new MemoryStream(data, OFFSET, size));
                                ////Console.WriteLine("   Recreated Value: {0}", value1);

                                const int OFFSET = 4;
                                resStream = new MemoryStream(data, OFFSET, data.Length - OFFSET);

                                break;
                            // Our only other type is DateTimeTZI.
                            default:
                                ////// No point in deserializing data if the type is unavailable.
                                ////if (dataType.Contains("DateTimeTZI") && loaded)
                                ////{
                                ////    BinaryFormatter binFmt = new BinaryFormatter();
                                ////    object value2 = binFmt.Deserialize(new MemoryStream(data));
                                ////    Console.WriteLine("   Recreated Value: {0}", value2);
                                ////}
                                ////break;
                                break;
                        }

                        // resStream = new MemoryStream(resData);
                    }
                }
            }

            return resStream;
        }

        // ******************************************************************
        private static void DebugPrintResources(System.Resources.ResourceReader reader)
        {
            Debug.Print("Begin dump resources: ---------------------");
            foreach (DictionaryEntry item in reader)
            {
                Debug.Print(item.Key.ToString());
            }
            Debug.Print("End   dump resources: ---------------------");
        }

        // ******************************************************************        // ******************************************************************
    }
}
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119