我可以使用以下命令检查行为是否已设置:
Attribute.GetCustomAttribute(typeof(MyService), typeof(ServiceBehavior))如何检查是否在ServiceBehavior属性中定义和设置了特定属性?例如,IncludeExceptionDetailInFaults:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]发布于 2015-11-14 04:29:11
实际上这很简单,我需要强制转换属性,然后检查属性:
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
{ }希望这对某些人有帮助。
https://stackoverflow.com/questions/33700386
复制相似问题