我可以检查是否设置了行为:
Attribute.GetCustomAttribute(typeof(MyService), typeof(ServiceBehavior))
如何检查是否在 ServiceBehavior 属性中定义和设置了特定属性?例如 IncludeExceptionDetailInFaults:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
我可以检查是否设置了行为:
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
{ }
希望这可以帮助某人。