简体   繁体   English

ReliabilityContractAttribute有什么作用?

[英]What does ReliabilityContractAttribute do?

Does it do anything at all or it is only for documentation. 它是做任何事情还是仅用于文档。 If it is only for documentation, why documentation doesn't document it? 如果它仅用于文档,为什么文档不记录它?

For example, these two static methods of System.Array : 例如, System.Array这两个静态方法:

[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
public static void Copy(Array sourceArray, Array destinationArray, int length)

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
public static void ConstrainedCopy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)

Their generated MSDN documentation doesn't even mention that Copy or ConstrainedCopy are annotated. 他们生成的MSDN文档甚至没有提到CopyConstrainedCopy被注释。

I believe they're used for constrained execution regions too, so the CLR knows what it can do safely. 我相信它们也被用于约束执行区域,因此CLR知道它可以安全地做什么。 There are certain things that your code can't do within the CER, and in return the CLR guarantees that certain out-of-band exceptions aren't thrown. 在CER中,您的代码无法执行某些操作,作为回报,CLR可以保证不会抛出某些带外异常。

MSDN has some more details. MSDN有更多细节。

From MSDN : 来自MSDN

The ReliabilityContractAttribute attribute provides a mechanism for you to document your code, and to indicate what type of reliability guarantees you can make in the face of exceptional conditions that could potentially lead to an inconsistent state. ReliabilityContractAttribute属性为您提供了一种记录代码的机制,并指出在面临可能导致状态不一致的特殊情况时可以保证的可靠性类型。 In this context, exceptional conditions are defined as asynchronous exceptions that can be generated at run time by the common language runtime, such as aborted threads, out-of-memory situations, and stack overflows. 在此上下文中,异常条件被定义为异步异常,它们可以在运行时由公共语言运行库生成,例如中止的线程,内存不足的情况和堆栈溢出。 You can apply the ReliabilityContractAttribute attribute to assemblies, types, and methods. 您可以将ReliabilityContractAttribute属性应用于程序集,类型和方法。

Use this attribute with the Consistency enumeration to define a reliability contract by documenting the level of reliability in a particular piece of code. 将此属性与Consistency枚举一起使用,通过记录特定代码段中的可靠性级别来定义可靠性合同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM