using System; using System.Collections.Generic; using System.Security.Cryptography; namespace MonoTorrent { public static class HashAlgoFactory { static Dictionary algos = new Dictionary(); static HashAlgoFactory() { Register(); Register(); } public static void Register() where T : HashAlgorithm where U : HashAlgorithm { Register(typeof(T), typeof(U)); } public static void Register(Type baseType, Type specificType) { Check.BaseType(baseType); Check.SpecificType(specificType); lock (algos) algos[baseType] = specificType; } public static T Create() where T : HashAlgorithm { if (algos.ContainsKey(typeof(T))) return (T)Activator.CreateInstance(algos[typeof(T)]); return null; } } }