示例#1
0
        /// <summary>
        /// Loads the specified manifest resource from this assembly.
        /// </summary>
        /// <param name="selector"></param>
        /// <returns>
        /// The manifest resource; or null if no resources were specified during compilation or if
        /// the resource is not visible to the caller.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="selector"/> parameter is null
        /// </exception>
        /// <exception cref="ArgumentException">
        /// The <paramref name="selector"/> parameter is an empty string
        /// </exception>
        /// <exception cref="FileLoadException">A file that was found could not be loaded.</exception>
        /// <exception cref="NotImplementedException">
        /// Resource length is greater than <see cref="Int64.MaxValue"/>
        /// </exception>
        public static byte[] GetManifestResource(Func <AssemblyResource, bool> selector)
        {
            if (selector == null)
            {
                throw new ArgumentNullException(nameof(selector));
            }

            var result = AssemblyAndResourceNamesInfo.FirstOrDefault(x => selector(x));

            if (result == null)
            {
                return(null);
            }

            using (var stream = result.Assembly.GetManifestResourceStream(result.Filename))
            {
                using (var memoryStream = new MemoryStream())
                {
                    memoryStream.SetLength(stream.Length);
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.CopyTo(memoryStream);
                    return(memoryStream.ToArray());
                }
            }
        }
示例#2
0
        /// <summary>
        /// Adds a new Assembly to the assembly collection
        /// </summary>
        /// <param name="assembly">The assembly to be added</param>
        /// <exception cref="NotSupportedException">
        /// <paramref name="assembly"/> is a dynamic assembly
        /// </exception>
        public static void AddAssembly(Assembly assembly)
        {
            if (assembly.IsDynamic)
            {
                throw new NotSupportedException("Dynamic assemblies are not supported");
            }

            if (_assemblies.Any(x => x.ManifestModule.Name.GetHashCode() == assembly.ManifestModule.Name.GetHashCode() && x.ManifestModule.Name == assembly.ManifestModule.Name))
            {
                return;
            }

            _assemblies.Add(assembly);
            object cauldronObject = null;

            foreach (var item in assembly.DefinedTypes)
            {
                if (item.FullName != null && item.FullName.GetHashCode() == CauldronClassName.GetHashCode() && item.FullName == CauldronClassName)
                {
#if NETFX_CORE || NETCORE
                    cauldronObject = Activator.CreateInstance(item.AsType());
#else
                    cauldronObject = Activator.CreateInstance(item);
#endif
                    _cauldron.Add(cauldronObject);
                }
            }

            AssemblyAndResourceNamesInfo.AddRange(assembly.GetManifestResourceNames().Select(x => new AssemblyResource(assembly, x)));
            LoadedAssemblyChanged?.Invoke(null, new AssemblyAddedEventArgs(assembly, cauldronObject));
        }
示例#3
0
        /// <summary>
        /// Returns all information about how the given resource has been persisted.
        /// </summary>
        /// <param name="resourceInfoName">
        /// The end of the fully qualified name of the embedded resource
        /// </param>
        /// <returns>
        /// An <see cref="List{T}"/> object that is populated with information about the resource's topology
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="resourceInfoName"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// The <paramref name="resourceInfoName"/> parameter is an empty string ("").
        /// </exception>
        public static List <AssemblyResource> GetManifestResources(string resourceInfoName)
        {
            if (resourceInfoName == null)
            {
                throw new ArgumentNullException(nameof(resourceInfoName));
            }

            if (resourceInfoName.Length == 0)
            {
                throw new ArgumentException("The parameter is an empty string", nameof(resourceInfoName));
            }

            return(AssemblyAndResourceNamesInfo.Where(x => x.Filename.EndsWith(resourceInfoName, StringComparison.OrdinalIgnoreCase)).ToList());
        }
示例#4
0
        /// <summary>
        /// Returns information about how the given resource has been persisted.
        /// </summary>
        /// <param name="resourceInfoName">
        /// The end of the fully qualified name of the embedded resource
        /// </param>
        /// <returns>
        /// An object that is populated with information about the resource's topology, or null if
        /// the resource is not found.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="resourceInfoName"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// The <paramref name="resourceInfoName"/> parameter is an empty string ("").
        /// </exception>
        public static ManifestResourceInfo GetManifestResourceInfo(string resourceInfoName)
        {
            if (resourceInfoName == null)
            {
                throw new ArgumentNullException(nameof(resourceInfoName));
            }

            if (resourceInfoName.Length == 0)
            {
                throw new ArgumentException("The parameter is an empty string", nameof(resourceInfoName));
            }

            var result = AssemblyAndResourceNamesInfo.FirstOrDefault(x => x.Filename.EndsWith(resourceInfoName, StringComparison.OrdinalIgnoreCase));

            if (result == null)
            {
                return(null);
            }

            return(result.Assembly.GetManifestResourceInfo(result.Filename));
        }