/// <summary> /// Add a resource to a specified uri without extension, ie user/test /// </summary> /// <param name="uri">The uri to add the resource to</param> /// <param name="info">The <see cref="ResourceInfo"/> instance describing the resource</param> private void AddResource(string uri, ResourceInfo info) { List<ResourceInfo> resources; if (!_loadedResources.TryGetValue(uri, out resources)) { resources = new List<ResourceInfo>(); _loadedResources.Add(uri, resources); } if (resources.Find(delegate(ResourceInfo resource) { return resource.Extension == info.Extension; }) != null) throw new InvalidOperationException(string.Format("A resource with the name '{0}.{1}' has already been added.", uri, info.Extension)); resources.Add(info); }
/// <summary> /// Loads resources from a namespace in the given assembly to an uri /// </summary> /// <param name="toUri">The uri to map the resources to</param> /// <param name="fromAssembly">The assembly in which the resources reside</param> /// <param name="fromNamespace">The namespace from which to load the resources</param> /// <usage> /// resourceLoader.LoadResources("/user/", typeof(User).Assembly, "MyLib.Models.User.Views"); /// /// will make ie the resource MyLib.Models.User.Views.list.Haml accessible via /user/list.haml or /user/list/ /// </usage> public void LoadResources(string toUri, Assembly fromAssembly, string fromNamespace) { toUri = toUri.ToLower().TrimEnd('/'); fromNamespace = fromNamespace.ToLower(); if (!fromNamespace.EndsWith(".")) fromNamespace += "."; foreach (string resourceName in fromAssembly.GetManifestResourceNames()) { if (resourceName.ToLower().StartsWith(fromNamespace)) { ResourceInfo info = new ResourceInfo(resourceName, fromAssembly); string uri = toUri + "/" + resourceName.Substring(fromNamespace.Length).ToLower().Replace('.', '/'); uri = uri.TrimStart('/'); if (!string.IsNullOrEmpty(info.Extension)) uri = uri.Substring(0, uri.Length - info.Extension.Length - 1); AddResource(uri, info); _logger.Info("Resource '" + info.Name + "' loaded to uri: " + uri); } } }