我可以检查是否使用设置了行为
Attribute.GetCustomAttribute(typeof(MyService), typeof(ServiceBehavior))
如何检查ServiceBehavior属性中是否定义和设置了特定属性?例如IncludeExceptionDetailInFaults:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
实际上很简单,我需要转换属性,然后检查属性:
Attribute behavior = Attribute.GetCustomAttribute(myService.GetType(), typeof(ServiceBehaviorAttribute));
if (behavior != null)
{
if (!((ServiceBehaviorAttribute)behavior).IncludeExceptionDetailInFaults)
{
throw new Exception(); // or whatever
}
}
该服务将类似于:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyService
{ }
希望这能帮助到别人。