Empeld
Empeld plugin documentation.
pluginbase.Objects.Tasks.TaskObjectExtensions Class Reference

Helper methods for binding task methods on objects by TaskAttribute More...

Static Public Member Functions

static ICollection< IScheduledTaskRegisterObjectTasks (this ITaskScheduler scheduler, object obj, Enum scope=null)
 Bind all TaskAttributes on a given object (Weakly, so if the object is disposed the tasks will stop running) More...
 

Detailed Description

Helper methods for binding task methods on objects by TaskAttribute

Member Function Documentation

◆ RegisterObjectTasks()

static ICollection<IScheduledTask> pluginbase.Objects.Tasks.TaskObjectExtensions.RegisterObjectTasks ( this ITaskScheduler  scheduler,
object  obj,
Enum  scope = null 
)
static

Bind all TaskAttributes on a given object (Weakly, so if the object is disposed the tasks will stop running)

Returns
The object tasks.
Parameters
schedulerScheduler.
scopeScope of the tasks to be bound
objObject to bind tasks to
22  {
23  var tasks = new List<IScheduledTask>();
24  foreach(var method in obj.GetType().GetAllMethods(x => Attribute.IsDefined(x, typeof(TaskAttribute))))
25  {
26  foreach(var attr in method.GetCustomAttributes(typeof(TaskAttribute), true).OfType<TaskAttribute>())
27  {
28  if (attr.HasScope(scope))
29  {
30  try
31  {
32  tasks.Add(scheduler.ScheduleTask(new WeakTask(method, obj, attr.Frequency, false)));
33  }
34  catch(Exception e)
35  {
36  obj.GetLogger().LogWarn($"Error binding task for {obj.GetType().FullName}:{method.Name} at {attr}");
37  }
38  }
39  }
40  }
41  return tasks;
42  }

The documentation for this class was generated from the following file: