[英]Apply Attribute with AttributeTargets = Method to all methods in a class
There is an Attribute
called DataSourceAttribute
in C#. 在C#中有一个名为
DataSourceAttribute
的Attribute
。 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 (...),...]
. 在Data Driven UnitTesting中,需要借助以下属性指定数据源:
[DataSource (...),...]
。 There are about 10-15 such methods, and I do not want to declare the attribute for each method. 大约有10-15个这样的方法,我不想为每个方法声明属性。 I'd like to declare it once and have all the methods inherit it from the class level usage.
我想声明一次并让所有方法从类级别的用法继承它。
Take a look at Afterthough . 看看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. 这是自定义属性的MSDN。 http://msdn.microsoft.com/en-us/library/sw480ze8(v=VS.100).aspx
http://msdn.microsoft.com/en-us/library/sw480ze8(v=VS.100).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.