R
R
Roman Rakzin2017-10-02 20:10:20
C++ / C#
Roman Rakzin, 2017-10-02 20:10:20

How to use attributes to apply a method to all other methods of a class in C#?

I want to implement validation (authorization and other calculations) for all class methods.
That is, when executing each method of the class, I want to first perform a certain check, and if it is passed, then execute the method further.
In fact, before each class method, write the execution of the check method.
Is it possible to implement this using attributes or abstract classes, or inheritance, so as not to write the execution of the check method every time, but to create a certain structure?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Eremin, 2017-10-02
@EreminD

If we are talking about asp.net, then there is a special attribute on this topic

E
eRKa, 2017-10-02
@kttotto

c# custom attribute example
Well, if this does not help, then there is the Attribute class, from which you need to inherit and make your own implementation.

public class MyCustomAttribute: Attribute
{
    public string SomeProperty { get; set; }
}

Then use
[MyCustom(SomeProperty = "foo bar")]
public class Foo
{

}

Y
Yuri Esin, 2017-10-03
@Exomode

It is more correct to do this through delegation or overriding, attributes are needed primarily to work with type metadata, implementing non-abstract business logic with their help (as I understand it, just what you want) is not the best approach.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question