c# – 如何对方法的属性存在进行NUnit测试
发布时间:2020-12-15 06:47:29 所属栏目:百科 来源:网络整理
导读:public interface IMyServer { [OperationContract] [DynamicResponseType] [WebGet(UriTemplate = "info")] string ServerInfo(); } 如何编写一个NUnit测试来证明C#接口方法中设置了[DynamicResponseType]属性? 解决方法 就像是: Assert.IsTrue(Attribute
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); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |