Existe uma maneira de salvar um método em uma variável e chamá-lo mais tarde? E se meus métodos retornarem tipos diferentes?
Edit: Obrigado pelas respostas. Atualmente, estou trabalhando nisso !! \
Tenho 3 métodos, S () retorna string, D () retorna double e B () retorna bool.
Eu também tenho uma variável que decide qual método eu uso. Eu quero fazer isso:
// I tried Func<object> method; but it says D() and B() don't return object.
// Is there a way to use Delegate method; ? That gives me an eror saying method group is not type System.Delegate
var method;
var choice = "D";
if(choice=="D")
{
method = D;
}
else if(choice=="B")
{
method = B;
}
else if(choice=="S")
{
method = S;
}
else return;
DoSomething(method); // call another method using the method as a delegate.
// or instead of calling another method, I want to do:
for(int i = 0; i < 20; i++){
SomeArray[i] = method();
}
Isso é possível
Li este post: Armazenamento de um método como variável de membro de uma classe em C # Mas preciso armazenar métodos com diferentes tipos de retorno ...