简体   繁体   中英

Apply Attribute with AttributeTargets = Method to all methods in a class

There is an Attribute called DataSourceAttribute in C#. It needs to be declared on each Method again and again. Is it possible to declare it one time at the class level so that I don't need to repeat myself. If so, how?

In Data Driven UnitTesting, the data source need to be specified with the help of this attribute: [DataSource (...),...] . There are about 10-15 such methods, and I do not want to declare the attribute for each method. I'd like to declare it once and have all the methods inherit it from the class level usage.

Take a look at Afterthough . It is a framework that applies custom chunks of code, including properties, methods, attributes to your solution post-compile.

You might be able to create a custom class attribute that applies the method attribute to all methods in the class.

Here is the MSDN for custom attributes. http://msdn.microsoft.com/en-us/library/sw480ze8(v=VS.100).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM