I am using reflection to invoke a method from a controller using a string in .NET core. I am given a C# object, item
, that contains the controller name, the method name, and the argument list.
Type type = Type.GetType("ExampleProject.Controllers." + item.route);
Object obj = Activator.CreateInstance(type);
MethodInfo methodInfo = type.GetMethod(item.name);
dynamic result = methodInfo.Invoke(obj, item.args);
This works fine, but I want to speed it up. What about creating a list of delegates? When the method is called the first time, we add a delegate for it to the list. The next time it is called, we just check the list and use the delegate (instead of reflection).
I understand that delegates and their associated method must have the same number and types of arguments, so making a delegate for an arbitrary method won't work. What I'm asking is: is there a way to make a delegate for methodInfo.Invoke(obj, item.args)
so that it can be called with delegateFunction(item.args)
?
Aucun commentaire:
Enregistrer un commentaire