Using Func with instance method
public class x : y
{
public Func<user, bool> SendStuffAction = SendStuff;
//overridden from y
public override bool SendStuff(user u)
{
//do stuff
}
}
Taking the above code, where SendStuff is an local overridden instance method, I get a context error that SendStuff not being static. Can't a delegate point to an instance method from inside the sa开发者_如何学Pythonme class to which the method SendStuff exists?
Error is:cannot access static method in a non-static context
What if the method is private, why would it not work then.
private Func<user, bool> SendStuffAction = SendStuff;
Yes, it can...but, you need to set it in the constructor if you do not declare as static.
class MyClass
{
public Func<loan, user, bool> SendStuffAction ;
MyClass()
{
SendStuffAction = SendStuff;
}
bool SendStuff(loan loanVar, user userVar)
{
return true;
}
}
精彩评论