Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a variable of type Func<...> is a specific class method

Tags:

c#

casting

func

I would like to check in runtime that a variable of type Func<...> is a specific class method. E.g.

class Foo
{
    public static int MyMethod(int a, int b)
    {
        //...
    }
}

Func<int, int, int> myFunc;
myFunc = Foo.MyMethod;

if(myFunc is Foo.MyMethod)
{
    //do something
}
like image 546
Sebastian Widz Avatar asked Nov 10 '14 19:11

Sebastian Widz


People also ask

How do you check if a variable is of a certain class?

Using isinstance() function, we can test whether an object/variable is an instance of the specified type or class such as int or list. In the case of inheritance, we can checks if the specified class is the parent class of an object. For example, isinstance(x, int) to check if x is an instance of a class int .

How do you check if a variable is a function?

To check if a variable is of type function, use the typeof operator, e.g. typeof myVariable === 'function' . The typeof operator returns a string that indicates the type of the value. If the type of the variable is a function, a string containing the word function is returned.

How do you check if a variable is a certain type in Python?

Use isinstance() to check if a variable is a certain type Call isinstance(variable, type) to check if variable is an instance of the class type .


1 Answers

You should be able to compare the two directly using ==:

if (myFunc == Foo.MyMethod) { ... }
like image 147
Andrew Whitaker Avatar answered Nov 15 '22 04:11

Andrew Whitaker