mardi 13 novembre 2018

Can I use an extension method to extend all methods? (For a timer, c#)

Problem: I have a timer class which given a method name, target, time and args will invoke the function later using reflection/MethodInfo. I was wondering, if there is a way to extend all Methods directly so I can have an end result call like

MyClass mc = new MyClass(); //My class has a function called MyMethod in it mc.MyMethod.AddToTimer(3); //Desired ability, but to be called on any class

Or the alternative mc.AddToTimer(mc.MyMethod) if extending a class is more viable.

I've tried among other things Public static void AddToTimer(this MethodInfo mi) But it only extends MethodInfos, what sort of signature is a method?

I could then use lazy initialization or attributes to save the methodInfos in a global dict to cut down on the cost of reflection, but thats besides the point.

Googling this is quite hard since "Extending a method, Extension methods on methods" closely relates to "Extension Method".





Aucun commentaire:

Enregistrer un commentaire