How to write a method that will be executed before every other method?

There is a method A and methods B,C,D,E.
Need A method to run before each method B,C,D,E, when they will be started and if method A is false, then to go out and run the following code.
Actually need to enter the method And in each method.
But is there some better way to wrap that would not prescribe the same actions in each of the methods and inherit class a (method A) and pass everything through it?
June 14th 19 at 20:44
5 answers
June 14th 19 at 20:46
Create your attribute, where inside will be to work out Your method and decelrate methods B,C,D,E this attribute. Every time before a method is run first will work the one in the attribute.
You don't understand attributes such functionality are not available. - Josef commented on June 14th 19 at 20:49
Via the attributes it is possible to make the decorator, but the method will have to call a smart-ass way, first look for the attribute using reflection, then get information out of him about another method and so on. - litzy commented on June 14th 19 at 20:52
What they have no functionality? ))

Three years ago I solved a similar problem when using attributes I have indicated how the method should work. How do you work the Authorize attribute? All Permision?

Of course you need code to add, inside of a class reflection to check the attributes and their values or a static method add. If just for features, it is possible to create a factory that will attribute to check, before giving the copy. A lot of options how to use it. You will be a week to look for ready-made, lamenting that such a feature is not, to fence the gardens than just to write 10 lines of code. And then compare the cost of the work. - Abner32 commented on June 14th 19 at 20:55
look for the beginning of the class System.Attribute, it doesn't even have any virtual method that is executed before executing the marked method. Authorize works, the router parses the query string, select the controller method, and then watching the presence attribute, verifies access and then just calls a method of controller, I specify, the router checks. Here's the AuthorizeAttribute source code there is not even a method, not then. Maybe you did not understand me, only assign attribute will not do anything, can't we understand each other, but the answer is that it is enough to write your own attribute to mark the methods and everything will work. - Josef commented on June 14th 19 at 20:58
the answer read comrade eRKa, sounds like, enough to write an attribute to mark the method and all. of course need some kind of wrapper, for example as is done in Fody.PropertyChanged, there is a code generation and implementation of code after compilation. - Josef commented on June 14th 19 at 21:01
I know how Authorize) , I use custom attributes to work. I don't really painted to trifles, but all right - "attribution only attribute won't do anything". I just gave a tip on how to solve this problem, with the promise that it people just zaguglit work with attributes.

So just
maybe we didn't understand each other
- Abner32 commented on June 14th 19 at 21:04
Ok, TC read, maybe come to a decision - Josef commented on June 14th 19 at 21:07
June 14th 19 at 20:48
class Sample {
 protected bool A() {
 return true; // Some logic value.

 public void B() {
 if (!A()) return;

 // Some code...

This is a classic functional approach.
static class SampleExtensions {
 public static void ExecuteWithPrepare(this Action action, Func<bool> predicate) {
 if (predicate == null || !predicate()) return;


And this is one example of implementation of simple delegation. Using extension methods. Through clear signatures of the delegates can be other options to come up, depends on the task and imagination.
June 14th 19 at 20:50
It's called a decorator, in sharp they are not. Don't know what you have specifically for the occasion, but sometimes it is convenient to make A method in a static class. Or you can make the method of mediation that you want to pass a delegate, and it will do the check and either call the delegate or not.
Still PostSharp knows how to make decorators with postprocess at compile time. - Josef commented on June 14th 19 at 20:53
It's called a decorator, in sharp they are not.

What do you mean? No factory, no Builder, no interpreter? All this is solved with standard solutions. - litzy commented on June 14th 19 at 20:56
Yes , the decorator pattern can be implemented, but if you compare with Python, support for decorators is weak. - Abner32 commented on June 14th 19 at 20:59
June 14th 19 at 20:52
get set value false {your methods}
June 14th 19 at 20:54
Possible, but difficult, need to use code generation as an example

Find more questions by tags C#