开发者

How to NUnit test for a method's attribute existence

   public interface IMyServer
    {
        [OperationContract]
        [DynamicResponseType]
        [WebGet(UriTemplate = "info")]
        string Se开发者_StackOverflow社区rverInfo();
    }

How do I write an NUnit test to prove that the C# interface method has the [DynamicResponseType] attribute set on it?


Something like:

Assert.IsTrue(Attribute.IsDefined(
            typeof(IMyServer).GetMethod("ServerInfo"),
            typeof(DynamicResponseTypeAttribute)));

You could also do something involving generics and delegates or expressions (instead of the string "ServerInfo"), but I'm not sure it is worth it.

For [WebGet]:

WebGetAttribute attrib = (WebGetAttribute)Attribute.GetCustomAttribute(
    typeof(IMyServer).GetMethod("ServerInfo"),
    typeof(WebGetAttribute));
Assert.IsNotNull(attrib);
Assert.AreEqual("info", attrib.UriTemplate);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜