public static Object GetSingleTon(Type objType) { string name = objType.Name; if (!SingleTons.ContainsKey(name)) { lock (_lock) { if (!SingleTons.ContainsKey(name)) { var types = Lib.Reflection.AssemblyLoader.LoadTypeByInterface(objType); //TODO: add rule to get the right intance. if (types != null && types.Any()) { var type = types[0]; var obj = Activator.CreateInstance(type); SingleTons[name] = obj; } else { SingleTons[name] = null; } } } } return(SingleTons[name]); }
public static Object GetSingleTon(Type objType, bool SearchImplementation) { string name = objType.Name; if (!SingleTons.ContainsKey(name)) { if (SearchImplementation) { lock (_lock) { if (!SingleTons.ContainsKey(name)) { var types = Lib.Reflection.AssemblyLoader.LoadTypeByInterface(objType); if (Lib.Reflection.TypeHelper.HasInterface(objType, typeof(IPriority))) { List <IPriority> list = new List <IPriority>(); foreach (var item in types) { var obj = Activator.CreateInstance(item) as IPriority; if (obj != null) { list.Add(obj); } } if (list.Any()) { SingleTons[name] = list.OrderByDescending(o => o.Priority).First(); } else { SingleTons[name] = null; } } else { if (types != null && types.Any()) { var type = types[0]; var obj = Activator.CreateInstance(type); SingleTons[name] = obj; } else { SingleTons[name] = null; } } } } } else { return(null); } } return(SingleTons[name]); }