I try to figure out how I can access a static method within CallMe<T>()
of class DoSomething
. Is reflection the only solution here? I do not want to instantiate an object of type MyAction
. Also if doing it through reflection is there a way to create the method through reflection within the method CallMe<T>()
just once and then calling it many times to perform multiple operations on the same "reflected" method? Or is there any better way than through reflection? I basically want to create template implementation style classes such as MyAction
that define how byte[] DoThis(string text)
performs its duty. The AskForSomething()
will then specify which template is being used and according to that the CallMe<T>()
will go about its work.
public class AskSomething
{
public void AskForSomething()
{
DoSomething doSomething = new DoSomething();
doSomething.CallMe<MyAction>();
}
}
public class DoSomething
{
public void CallMe<T>()
{
Type type = typeof(T);
//Question: How can I access 'DoThis(string text)' here?
//Most likely by reflection?
}
}
public class MyAction
{
public static byte[] DoThis(string text)
{
byte[] ret = new byte[0]; //mock just to denote something is done and out comes a byte array
return ret;
}
}