Represents a file object in embedded resource space.

This type is used by the GSF.Web.Hosting.EmbeddedResourcePathProvider to serve embedded resources as virtual files. The properties on the file can be used to retrieve an embedded resource stream from the GSF.Web.Hosting.EmbeddedResourceVirtualFile.ContainingAssembly at the appropriate GSF.Web.Hosting.EmbeddedResourceVirtualFile.ResourcePath using reflection. As part of the System.Web.Hosting.VirtualFile interface, you can do this easily via GSF.Web.Hosting.EmbeddedResourceVirtualFile.Open.

For more information on embedded resource virtual filesystem usage, see GSF.Web.Hosting.EmbeddedResourcePathProvider

Inheritance: System.Web.Hosting.VirtualFile
示例#1
0
        /// <summary>
        /// Reads in the embedded files from an assembly an processes them into
        /// the virtual file system.
        /// </summary>
        /// <param name="assemblyName">The name of the <see cref="System.Reflection.Assembly"/> to load and process.</param>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="assemblyName" /> is <see langword="null" />.
        /// </exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Thrown if <paramref name="assemblyName" /> is <see cref="System.String.Empty" />.
        /// </exception>
        /// <exception cref="System.IO.FileNotFoundException">
        /// Thrown if the <see cref="System.Reflection.Assembly"/> indicated by
        /// <paramref name="assemblyName" /> is not found.
        /// </exception>
        /// <remarks>
        /// <para>
        /// The <paramref name="assemblyName" /> will be passed to <see cref="System.Reflection.Assembly.Load(string)"/>
        /// so the associated assembly can be processed.  If the assembly is not
        /// found, a <see cref="System.IO.FileNotFoundException"/> is thrown.
        /// </para>
        /// <para>
        /// Once the assembly is retrieved, it is queried for <see cref="GSF.Web.Hosting.EmbeddedResourceFileAttribute"/>
        /// instances.  For each one found, the associated resources are processed
        /// into virtual files that will be stored in
        /// <see cref="GSF.Web.Hosting.EmbeddedResourcePathProvider.Files"/>
        /// for later use.
        /// </para>
        /// </remarks>
        /// <seealso cref="GSF.Web.Hosting.EmbeddedResourcePathProvider" />
        /// <seealso cref="GSF.Web.Hosting.EmbeddedResourcePathProvider.Initialize" />
        protected virtual void ProcessEmbeddedFiles(string assemblyName)
        {
            if (string.IsNullOrEmpty(assemblyName))
            {
                throw new ArgumentNullException(nameof(assemblyName));
            }

            Assembly assembly = Assembly.LoadFrom(FilePath.GetAbsolutePath(assemblyName));

            // Get the embedded files specified in the assembly; bail early if there aren't any.
            EmbeddedResourceFileAttribute[] attribs = (EmbeddedResourceFileAttribute[])assembly.GetCustomAttributes(typeof(EmbeddedResourceFileAttribute), true);
            if (attribs.Length == 0)
            {
                return;
            }

            // Get the complete set of embedded resource names in the assembly; bail early if there aren't any.
            List <string> assemblyResourceNames = new List <string>(assembly.GetManifestResourceNames());

            if (assemblyResourceNames.Count == 0)
            {
                return;
            }

            foreach (EmbeddedResourceFileAttribute attrib in attribs)
            {
                // Ensure the resource specified actually exists in the assembly
                if (!assemblyResourceNames.Contains(attrib.ResourcePath))
                {
                    continue;
                }

                // Map the path into the web application
                string mappedPath;
                try
                {
                    mappedPath = VirtualPathUtility.ToAbsolute(MapResourceToWebApplication(attrib.ResourceNamespace, attrib.ResourcePath));
                }
                catch (ArgumentNullException)
                {
                    continue;
                }
                catch (ArgumentOutOfRangeException)
                {
                    continue;
                }

                // Create the file and ensure it's unique
                EmbeddedResourceVirtualFile file = new EmbeddedResourceVirtualFile(mappedPath, assembly, attrib.ResourcePath);
                if (this.Files.Contains(file.VirtualPath))
                {
                    continue;
                }

                // The file is unique; add it to the filesystem
                this.Files.Add(file);
            }
        }
示例#2
0
        /// <summary>
        /// Reads in the embedded files from an assembly an processes them into
        /// the virtual file system.
        /// </summary>
        /// <param name="assemblyName">The name of the <see cref="System.Reflection.Assembly"/> to load and process.</param>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="assemblyName" /> is <see langword="null" />.
        /// </exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Thrown if <paramref name="assemblyName" /> is <see cref="System.String.Empty" />.
        /// </exception>
        /// <exception cref="System.IO.FileNotFoundException">
        /// Thrown if the <see cref="System.Reflection.Assembly"/> indicated by
        /// <paramref name="assemblyName" /> is not found.
        /// </exception>
        /// <remarks>
        /// <para>
        /// The <paramref name="assemblyName" /> will be passed to <see cref="System.Reflection.Assembly.Load(string)"/>
        /// so the associated assembly can be processed.  If the assembly is not
        /// found, a <see cref="System.IO.FileNotFoundException"/> is thrown.
        /// </para>
        /// <para>
        /// Once the assembly is retrieved, it is queried for <see cref="GSF.Web.Hosting.EmbeddedResourceFileAttribute"/>
        /// instances.  For each one found, the associated resources are processed
        /// into virtual files that will be stored in
        /// <see cref="GSF.Web.Hosting.EmbeddedResourcePathProvider.Files"/>
        /// for later use.
        /// </para>
        /// </remarks>
        /// <seealso cref="GSF.Web.Hosting.EmbeddedResourcePathProvider" />
        /// <seealso cref="GSF.Web.Hosting.EmbeddedResourcePathProvider.Initialize" />
        protected virtual void ProcessEmbeddedFiles(string assemblyName)
        {
            if (string.IsNullOrEmpty(assemblyName))
                throw new ArgumentNullException("assemblyName");

            Assembly assembly = Assembly.LoadFrom(FilePath.GetAbsolutePath(assemblyName));

            // Get the embedded files specified in the assembly; bail early if there aren't any.
            EmbeddedResourceFileAttribute[] attribs = (EmbeddedResourceFileAttribute[])assembly.GetCustomAttributes(typeof(EmbeddedResourceFileAttribute), true);
            if (attribs.Length == 0)
            {
                return;
            }

            // Get the complete set of embedded resource names in the assembly; bail early if there aren't any.
            List<String> assemblyResourceNames = new List<string>(assembly.GetManifestResourceNames());
            if (assemblyResourceNames.Count == 0)
            {
                return;
            }

            foreach (EmbeddedResourceFileAttribute attrib in attribs)
            {
                // Ensure the resource specified actually exists in the assembly
                if (!assemblyResourceNames.Contains(attrib.ResourcePath))
                {
                    continue;
                }

                // Map the path into the web application
                string mappedPath;
                try
                {
                    mappedPath = VirtualPathUtility.ToAbsolute(MapResourceToWebApplication(attrib.ResourceNamespace, attrib.ResourcePath));
                }
                catch (ArgumentNullException)
                {
                    continue;
                }
                catch (ArgumentOutOfRangeException)
                {
                    continue;
                }

                // Create the file and ensure it's unique
                EmbeddedResourceVirtualFile file = new EmbeddedResourceVirtualFile(mappedPath, assembly, attrib.ResourcePath);
                if (this.Files.Contains(file.VirtualPath))
                {
                    continue;
                }

                // The file is unique; add it to the filesystem
                this.Files.Add(file);
            }
        }