c# – 如何对方法的属性存在进行NUnit测试

前端之家收集整理的这篇文章主要介绍了c# – 如何对方法的属性存在进行NUnit测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public interface IMyServer
    {
        [OperationContract]
        [DynamicResponseType]
        [WebGet(UriTemplate = "info")]
        string ServerInfo();
    }

如何编写一个NUnit测试来证明C#接口方法中设置了[DynamicResponseType]属性

解决方法

就像是:
Assert.IsTrue(Attribute.IsDefined(
            typeof(IMyServer).GetMethod("ServerInfo"),typeof(DynamicResponseTypeAttribute)));

你也可以做一些涉及泛型和委托或表达式的事情(而不是字符串“ServerInfo”),但我不确定它是值得的.

对于[WebGet]:

WebGetAttribute attrib = (WebGetAttribute)Attribute.GetCustomAttribute(
    typeof(IMyServer).GetMethod("ServerInfo"),typeof(WebGetAttribute));
Assert.IsNotNull(attrib);
Assert.AreEqual("info",attrib.UriTemplate);
原文链接:https://www.f2er.com/csharp/94624.html

猜你在找的C#相关文章