/// <summary> /// Protected constructor. /// </summary> /// <param name="root">The root directory or <c>null</c> if this is the root.</param> /// <param name="parent">The parent directory or <c>null</c> for the root directory.</param> /// <param name="name">The directory name (this must be <c>null</c> for the root directory.</param> protected StaticDirectoryBase(StaticDirectoryBase root, StaticDirectoryBase parent, string name) { Covenant.Requires <ArgumentException>(name.IndexOf('/') == -1, nameof(name)); Covenant.Requires <ArgumentException>(name.IndexOf('\\') == -1, nameof(name)); if (name.StartsWith("/")) { name = name.Substring(1); } if (root == null) { if (parent != null) { throw new ArgumentNullException($"[{nameof(parent)}] must be NULL when [{nameof(root)}] is NULL."); } if (!string.IsNullOrEmpty(name)) { throw new ArgumentNullException($"[{nameof(name)}] must be NULL or empty when [{nameof(root)}] is NULL."); } } else { Covenant.Requires <ArgumentNullException>(parent != null); Covenant.Requires <ArgumentNullException>(!string.IsNullOrEmpty(name)); } this.root = root ?? this; this.Parent = parent; this.Name = name; this.Path = GetPath(); this.nameToDirectory = new Dictionary <string, StaticDirectoryBase>(StringComparer.InvariantCultureIgnoreCase); this.nameToFile = new Dictionary <string, StaticFileBase>(StringComparer.InvariantCultureIgnoreCase); }
/// <summary> /// Adds a subdirectory if it doesn't already exist. /// </summary> /// <param name="directory">The child resource directory.</param> /// <returns>The existing <see cref="StaticResourceDirectory"/> or <paramref name="directory"/> if it was added</returns> public StaticDirectoryBase AddDirectory(StaticDirectoryBase directory) { lock (syncLock) { if (nameToDirectory.TryGetValue(directory.Name, out var existingDirectory)) { return(existingDirectory); } nameToDirectory.Add(directory.Name, directory); return(directory); } }