/// <summary> /// This function will find all Singleton classes, instantiate them /// and call their 'initialize' methods in order of Priority. /// </summary> public static void Initialize() { // load assemblies - only once LoadAssemblies(); // get all singleton types ArrayRig <Type> singletonTypes = new ArrayRig <Type>(); foreach (var type in Generic.GetTypes(typeof(ISingleton))) { singletonTypes.Add(type); } FuncSet <ISingleton>[] constructors = new FuncSet <ISingleton> [singletonTypes.Count]; ArrayRig <ISingleton> singles = new ArrayRig <ISingleton>(singletonTypes.Count); // get singleton constructors for (int i = 0; i < singletonTypes.Count; ++i) { constructors[i] = Dynamic.Constructor <FuncSet <ISingleton> >(singletonTypes[i]); } // create all singletons for (int i = 0; i < constructors.Length; ++i) { singles.Add(constructors[i].Func()); } // sort singletons in order of decending priority singles.Sort((a, b) => Generic.GetValue <byte>(a, "SingletonPriority") > Generic.GetValue <byte>(b, "SingletonPriority")); // get the array _singletons = singles.ToArray(); // initialize in decending order of priority for (int i = 0; i < _singletons.Length; ++i) { // invoke generic initialize method Type type = _singletons[i].GetType(); type.BaseType.GetMethod("initialize", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(_singletons[i], null); Log.Info("Created " + type.Name); } }
/// <summary> /// Add a needle to this handle. The handle will retrieve tasks from the needles it knows about in order /// of priority. /// </summary> public void Add(Needle _needle) { Needles.Add(_needle); Needles.Sort((a, b) => a.Priority < b.Priority); }