I have a lot of experience with PHP, and as I'm starting with C # now, it's going to be common for me to want to compare one language with another to find out if I can do something similar.
In PHP, I can add a functionality to a class so that, when the method does not exist, an action is performed. This is the magic __call
method. That is, it adds a dynamic call to methods that have not been defined in the class based on what is set to __call
.
According to the Manual:
__ call () is triggered when invoking inaccessible methods in an object context.
For example,
class Dynamic {
public function action() {
return 'existe';
}
public function __call($method, $parameters) {
return "Não existe, mas farei alguma coisa com {$method}";
}
}
$d = new Dynamic();
$d->action(); // 'existe'
$d->naoExisteEsseMetodo(); // 'Não existe, mas farei alguma coisa com naoExisteEsseMethodo'
Note that the last named method does not exist, but "something is done" anyway.
Is there any way to create functionality similar to the __call
of PHP in C #?