using System; using System.Collections.Generic; using System.Reflection; namespace Beehive.CloudReader.Utility { /// <summary> /// 反射工厂 /// </summary> public abstract class FactoryBase { /// <summary> /// /// </summary> protected FactoryBase() { } /// <summary> /// /// </summary> protected static readonly SortedList<string, Type> Items = new SortedList<string, Type>(); /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> protected static T CreateType<T>() { if (Items.ContainsKey(typeof(T).FullName)) { return (T)Activator.CreateInstance<T>(); } var attributes = typeof(T).GetCustomAttributes(typeof(ConcreteTypeAttribute), true); if (attributes.Length > 0 && attributes[0] is ConcreteTypeAttribute) { var facadeName = ((ConcreteTypeAttribute)attributes[0]).TypeName; var targetType = Type.GetType(facadeName); lock (Items) { Items[typeof(T).FullName] = targetType; } return (T)Activator.CreateInstance(targetType); } return default(T); } /**Type t = typeof(string); Type t = typeof(System.String); Type 是抽象类, typeof(类名称) 返回的是继承自Type 的RuntimeType*/ /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <param name="FullName"></param> /// <returns></returns> protected static T Create<T>(string path, string FullName) { return (T)Assembly.Load(path).CreateInstance(FullName); } } }