TSFac注入方式:
泛型接口工厂:
public class SFac<TInterface, TClass> where TInterface : class where TClass : TInterface { private static TInterface _instance; public static TInterface Instance { get { TInterface instanceTmp = Activator.CreateInstance<TClass>();// TClass必须存在公共的构造函数 if (_instance == null || _instance.GetType().FullName != instanceTmp.GetType().FullName) { _instance = instanceTmp; } return _instance; } } }
泛型单例接口模式调用:
SFac<Interface, Class>.Instance.InterfaceMethod()
泛型类型工厂:
public class TFac<TClass> where TClass : class, new() { private static TClass _instance; public static TClass Instance { get { if (_instance == null) { _instance = Activator.CreateInstance<TClass>();// TClass必须存在公共的构造函数 } return _instance; } } }
泛型单例对象模式调用:
TFac<Class>.Instance.Method()
以上方法已成熟使用过.