using System; using System.Collections.Generic; using System.Reflection; using DiscUtils.CoreCompat; using DiscUtils.Internal; namespace DiscUtils { /// /// Helps discover and use VirtualDiskFactory's /// public static class VirtualDiskManager { static VirtualDiskManager() { ExtensionMap = new Dictionary(); TypeMap = new Dictionary(); DiskTransports = new Dictionary(); } internal static Dictionary DiskTransports { get; } internal static Dictionary ExtensionMap { get; } /// /// Gets the set of disk formats supported as an array of file extensions. /// public static ICollection SupportedDiskFormats { get { return ExtensionMap.Keys; } } /// /// Gets the set of disk types supported, as an array of identifiers. /// public static ICollection SupportedDiskTypes { get { return TypeMap.Keys; } } internal static Dictionary TypeMap { get; } /// /// Locates VirtualDiskFactory factories attributed with VirtualDiskFactoryAttribute, and types marked with VirtualDiskTransportAttribute, that are able to work with Virtual Disk types. /// /// An assembly to scan public static void RegisterVirtualDiskTypes(Assembly assembly) { foreach (Type type in assembly.GetTypes()) { VirtualDiskFactoryAttribute diskFactoryAttribute = (VirtualDiskFactoryAttribute)ReflectionHelper.GetCustomAttribute(type, typeof(VirtualDiskFactoryAttribute), false); if (diskFactoryAttribute != null) { VirtualDiskFactory factory = (VirtualDiskFactory)Activator.CreateInstance(type); TypeMap.Add(diskFactoryAttribute.Type, factory); foreach (string extension in diskFactoryAttribute.FileExtensions) { ExtensionMap.Add(extension.ToUpperInvariant(), factory); } } VirtualDiskTransportAttribute diskTransportAttribute = ReflectionHelper.GetCustomAttribute(type, typeof(VirtualDiskTransportAttribute), false) as VirtualDiskTransportAttribute; if (diskTransportAttribute != null) { DiskTransports.Add(diskTransportAttribute.Scheme.ToUpperInvariant(), type); } } } } }