Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

[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.

like image 470
Prankster Avatar asked Apr 14 '09 16:04

Prankster


2 Answers

I believe they're used for constrained execution regions too, so the CLR knows what it can do safely. 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.

MSDN has some more details.

like image 102
Jon Skeet Avatar answered Nov 06 '22 10:11

Jon Skeet


From 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. 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.

Use this attribute with the Consistency enumeration to define a reliability contract by documenting the level of reliability in a particular piece of code.

like image 25
Andrew Hare Avatar answered Nov 06 '22 10:11

Andrew Hare