roundcrisis
roundcrisis

Reputation: 17795

Find the name of the delegate

Say that I have the following class

public class Conditional 
{
    private readonly Func<Boolean> _conditional;

    public Conditional(Func<Boolean> test)
    {
        _conditional = test;
    }

    public override BehaviourReturnCode Behave()
    {
        var conditionalResult = _conditional.Invoke();
 //... keeps going
 }

so when I create an instance of Conditional, I would do something like

var isAlive = new Conditional(actor.IsAlive);

so the question is, how can I find out the method name of _conditional , in this case IsAlive ?

FYI _conditional.Method.Name doesn't do it :D

Cheers

Upvotes: 0

Views: 93

Answers (1)

Trevor Elliott
Trevor Elliott

Reputation: 11252

Consider the following:

Func<bool> UserIsAlive = () =>
    {
        return false;
    };

Func<bool> UserIsDead = UserIsAlive;

You have two local variables which reference the same lambda function.

Which one is the real name? UserIsAlive or UserIsDead?

Hint: It doesn't have a name. It's a function. If you want to name it in some way that's up to you to decide how you should do so. Wrap it in a class? Store it in a dictionary? Pass the name to the function like so?

var isAlive = new Conditional(actor.IsAlive, "IsAlive");

Upvotes: 4

Related Questions