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
}
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 .
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.
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 .
You should be able to compare the two directly using ==
:
if (myFunc == Foo.MyMethod) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With