Context
I am writing an app which can call functions at runtime with the given function name.
The following setup could be an example:
internal class Caller
{
public static Dictionary<string, Func<object, object>> reg = new Dictionary<string, Func<object, object>>();
static Caller()
{
reg.Add("example1", Funcs.example1);
reg.Add("example2", Funcs.example2);
}
public object Call(string name, object obj)
{
return reg[name](obj);
}
}
internal static class Funcs
{
public static object example1(object obj)
{
// do sth
return obj;
}
public static object example2(object obj)
{
// do sth else
return obj;
}
}
Problem
The Problem with this is that my code isn't expandable. It would be difficult to register more functions at runtime/in another assembly, and it is a lot of typing for each function.
Coming from Python, I am used to using @decorators. They could be applied on every function I want to register, and then it would register it automatically.
In c# we have [Attributes], so I am thinking about giving each function a [CustomAttribute] and in my static Caller() I loop through every assembly, every static class in the assembly, and then register all functions with the [CustomAttribute].
I don't know whether this is ok to do from a design and performance standpoint.
I know of [HttpGet] from ASP.NET, what approach do they use? (Couldn't find it in the source). Is there perhaps a design pattern I am missing?