fakeNoose
Member
- Joined
- Sep 19, 2019
- Messages
- 10
- Programming Experience
- Beginner
Hey Everyone
I'm using the below to find all children classes of an abstract base class and then instantiate each of them and add that instance to a list.
What I'm wanting to be able to do is loop through that list of instances and run a Task that is part of the abstract base class for each instance every second, but I only want to run that task if the previous task run a second ago is completed. I've seen others do something similar using Func<Task> but I have no idea how they implemented that.
Is this something that is possible to do and if so how should I go about doing it?
Thank you in advance!!
I'm using the below to find all children classes of an abstract base class and then instantiate each of them and add that instance to a list.
What I'm wanting to be able to do is loop through that list of instances and run a Task that is part of the abstract base class for each instance every second, but I only want to run that task if the previous task run a second ago is completed. I've seen others do something similar using Func<Task> but I have no idea how they implemented that.
Is this something that is possible to do and if so how should I go about doing it?
Thank you in advance!!
C#:
public static List<ConsumableBase> Instances = new List<ConsumableBase>();
public static async void Manage()
{
IEnumerable<Type> _classes = GetChildrenOfType(typeof(ConsumableBase));
foreach (Type _class in _classes)
{
ConsumableBase _obj = (ConsumableBase) InstantiateType(_class);
Instances.Add(_obj);
}
}
private static IEnumerable<Type> GetChildrenOfType(Type _type)
{
var _classes = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => _type.IsAssignableFrom(p));
return _classes;
}
private static object InstantiateType(Type _type)
{
var _ctors = _type.GetConstructors(BindingFlags.Public);
var _inst = _ctors[0].Invoke(new object[] { });
return _inst;
}