How to NUnit test for a method's attribute existence

attributes, c#, nunit

Solution

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);

Problem

``` public interface IMyServer { [OperationContract] [DynamicResponseType] [WebGet(UriTemplate = "info")] string ServerInfo(); } ``` How do I write an NUnit test to prove that the C# interface method has the `[DynamicResponseType]` attribute set on it?

Original source