简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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