jjzjj

SmartDev-Contract

全部标签

c# - 在代码合约中使用 Contract.ForAll

好的,我还有另一个代码契约(Contract)问题。我有一个看起来像这样的接口(interface)方法的契约(Contract)(为清楚起见省略了其他方法):[ContractClassFor(typeof(IUnboundTagGroup))]publicabstractclassContractForIUnboundTagGroup:IUnboundTagGroup{publicIUnboundTagGroup[]GetAllGroups(){Contract.Ensures(Contract.Result()!=null);Contract.Ensures(Contract.F

c# - 代码契约(Contract) : Why are some invariants not considered outside the class?

考虑这个不可变类型:publicclassSettings{publicstringPath{get;privateset;}[ContractInvariantMethod]privatevoidObjectInvariants(){Contract.Invariant(Path!=null);}publicSettings(stringpath){Contract.Requires(path!=null);Path=path;}}这里需要注意两点:有一个保证Path属性永远不会为null的契约不变量构造函数检查path参数值以遵守先前的契约不变量此时,Setting实例永远不能有n

c# - 代码契约(Contract)不能反转条件?

我有这个结构(为简洁起见简化):publicstructPeriod{publicPeriod(DateTime?start,DateTime?end):this(){if(end.HasValue&&start.HasValue&&end.Value但是静态检查器给了我这个警告:CodeContracts:requiresunproven:end.HasValue&&start.HasValue&&end.Value>=start.Value它从自定义参数验证中推断出的这个要求完全是错误的。我想为start允许空值或end,并且只需要start如果两者都提供。但是,如果我将构造函数更

c# - 我可以将契约(Contract)留在代码中,以便与非代码契约(Contract)开发人员使用的代码库合并吗?

在过去的几个月里,我一直在为我的公司开发一个副项目,但现在上级决定它非常适合现有产品。我一直在使用Microsoft的CodeContracts开发用于静态类型检查的辅助项目(部分原因是我以前没有使用过它们并且渴望学习)。我的问题是,如果我将我的代码checkin到代码库中并使用Contracts,是否所有其他开发人员都需要安装CodeContracts工具才能继续开发?我知道他们都没有安装它,而且我是这里的初级人员,所以我怀疑我能否说服他们所有人都安装它。我使用的是.Net4.5,因此包含了代码契约库,但我想知道VisualStudio是否会提示它们没有使用CONTRACTS_FUL

c# - 自动实现属性的代码契约(Contract)

有什么方法可以在.NET中将契约放在自动实现的属性上吗?(如果答案是"is"怎么办)?(我假设使用来自DevLabs的.NET代码契约(Contract)) 最佳答案 是的,这是可能的-所需要的只是将您的契约(Contract)条件添加到类中的[ContractInvariantMethod]方法中,然后添加等效的Requires前提条件到自动setter,并将后置条件Ensures添加到get。来自Reference的第2.3.1节Astheexampleillustrates,invariantsonauto-propertie

c# - 代码契约(Contract)的好处

我为什么要使用像这样的代码契约Contract.Requires(x!=null,"x");而不是旧的if(x!=null){}elsethrow...除了简洁还有其他好处吗? 最佳答案 根据MSDN:Thebenefitsofcodecontractsincludethefollowing:Improvedtesting:Codecontractsprovidestaticcontractverification,runtimechecking,anddocumentationgeneration.Automatictesting

c# - 确保 IEnumerable 不为空的契约(Contract)

给定的代码staticpublicintQ(){returnEnumerable.Range(0,100).Select(i=>i).First();}发出以下警告:warning:CodeContracts:requiresunproven:Any(source)如果我删除.Select()子句,它就会消失。但我不清楚我到底需要什么.Ensure才能满足cccheck。 最佳答案 你能用这段代码避免警告吗?varres=Enumerable.Range(0,100).Select(i=>i).Take(1);//executeon

c# - 用于 Salesforce 出站消息传递的契约(Contract)优先 WCF

我正在考虑为SalesforceOutboundMessaging实现监听器应用程序。walkthrough使用已弃用的ASMXWeb服务实现它。代码是使用带有/serverInterface开关的wsdl.exe生成的。这是SalesforceOutboundMessaging的wsdl。Processanumberofnotifications.NotificationServiceImplementationtldr是我需要实现NotificationBinding,以便Salesforce可以在他们的系统上发生事件时调用我的网络服务。我后来意识到svcutil本身并不支持契约优

c# - 与 Contract.Requires<T> 相比抛出异常?

我想知道我是应该抛出异常还是调用Contract.Requires例如:publicstaticvoidFunction(Stringstr){if(str==null)thrownewArgumentNullException("str","Inputstringcannotbenull.");//...}对比publicstaticvoidFunction(Stringstr){Contract.Requires(str!=null,"Inputstringcannotbenull.");//...}自Contract.Requires不需要CONTRACTS_FULL符号我也可以

c# - 我是否错误地实现了这个简单的契约(Contract)?

这是我的代码:publicclassRegularPolygon{publicintVertexCount;publicdoubleSideLength;publicRegularPolygon(intvertexCount,doublesideLength){Contract.Requires(vertexCount>=3);VertexCount=vertexCount;SideLength=sideLength;}[ContractInvariantMethod]privatevoidRegularPolygonInvariants(){Contract.Invariant(Ve