/// <summary> /// 获取已注册晚加载类型,通过反射机制创建该类型实例 /// 如果类型T没有注册,将抛出LazyLoadingException /// </summary> /// <typeparam name="T">接口类型或抽象类</typeparam> /// <returns>实现intfType的对象</returns> public static T New <T>() { var list = (ImplCollection)LazyLoadings[typeof(T)]; if (list == null || list.Count == 0) { var ex = new Exception(typeof(T).FullName + "没有注册实现类"); log.Error("没有注册实现类", ex); throw ex; } return((T)list.GetHighestImpl()); }
internal virtual void RegisterAll() { allReg.Sort((x, y) => x.ExtensionAttribute.Order.CompareTo(y.ExtensionAttribute.Order)); foreach (var item in allReg) { try { _Register(item.Assembly, item.ExtensionAttribute); } catch (Exception ex) { var message = string.Format("程序集{0}中注册属性{1}时发生错误。", item.Assembly, item.ExtensionAttribute); ex = new Exception(message, ex); log.Error(message, ex); if (OnErrorBreak(ex)) { throw ex; } } } }
/// <summary> /// 加载时执行 /// </summary> protected internal override void OnLoad() { foreach (var initType in _types) { try { RuntimeHelpers.RunClassConstructor(initType.TypeHandle); /* * 如果initType是抽象的,cctor将不会被执行 * 如果initType是继承自某类,如果没有自己的cctor或者静态成员访问父类,则父类的cctor不会执行 * */ } catch (TypeInitializationException ex) { Debug.WriteLine(ex.ToString()); log.Error(ex); } } _types.Clear(); base.OnLoad(); }