简体   繁体   English

将AttributeTargets = Method应用于类中的所有方法

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

There is an Attribute called DataSourceAttribute in C#. 在C#中有一个名为DataSourceAttributeAttribute 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.

相关问题 如何从接口AttributeUsage = AttributeTargets.Method中读取属性值 - How to read attribute value from interface AttributeUsage = AttributeTargets.Method 将PostSharp方面应用于类中的所有方法以记录方法名称 - Apply a PostSharp aspect to all methods in class to Log method name 有没有办法将[TestMethod]应用于所有带有类的方法 - Is there a way to apply [TestMethod] to all methods withing a class 将WCF属性应用于服务中的所有方法 - Apply a WCF attribute to all method in the service 将使用try {} catch包装类中所有方法的属性 - attribute that will wrap all methods in class with try { } catch 在类的所有方法之前运行一个方法 - Run a method before all methods of a class 在 c# 中获取所有具有自定义属性的方法永远找不到方法 - Getting all methods with custom attribute in c# never finds a method 在类和方法中同时应用属性时,如何强制使用方法属性? - When apply an attribute in class and method together, How to force use method attribute? 对基础 class 中的所有虚拟方法实现应用相同的覆盖 - Apply same override to all implementations of virtual method in base class 使用 AttributeTargets.Class 对自定义 ValidationAttribute 进行客户端验证 - Client-side validation for custom ValidationAttribute with AttributeTargets.Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM