2015-11-13 52 views
0

我可以檢查行爲設置或不:如何測試IncludeExceptionDetailInFaults

Attribute.GetCustomAttribute(typeof(MyService), typeof(ServiceBehavior)) 

如何檢查,如果一個特定的屬性是ServiceBehavior內定義和設置屬性?例如IncludeExceptionDetailInFaults:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)] 

回答

0

其實這很簡單,我需要投的屬性,然後檢查屬性:

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 
{ } 

希望這可以幫助某人。