Represents the base class for all memory assertion attributes.
Namespace:
SciTech.NetMemProfilerAssembly: MemProfiler2 (in MemProfiler2.dll)
Syntax
| C# |
|---|
public abstract class AssertionAttribute : Attribute |
| Visual Basic |
|---|
Public MustInherit Class AssertionAttribute _ Inherits Attribute |
| Visual C++ |
|---|
public ref class AssertionAttribute abstract : public Attribute |
Remarks
The assertion attributes can be applied to any method and
are used to declaratively define the expected memory usage of the method.
When running the method under .NET Memory Profiler, the profiler will insert memory assertions that will assert the expected memory usage automatically.
The assertion attributes map to the methods in the AssertionsDefinition class.The following memory assertion attribute classes are available:
Examples
The following example shows a method that loads a bitmap using a dialog.
using System.Drawing; using SciTech.NetMemProfiler; [NoNewInstances(“System.Windows.Forms.*”, IncludeSubclasses=true)] [NoNewInstances(“System.Drawing.*”, IncludeSubclasses=true)] [MaxNewInstances( typeof( Bitmap), 1] Bitmap LoadBitmap() { using( LoadBitmapDialog dlg = new LoadBitmapDialog() ) { dlg.ShowDialog(); return dlg.LoadedBitmap; } } |
The applied attributes indicate that:
- When the method returns no new instances of any type in the System.Windows.Forms namespace has been created, including all types derived from a type in the System.Windows.Forms namespace.
- When the method returns no new instances of any type in the System.Drawing namespace (including all derived types) has been created, except...
- ... a single instance of the Bitmap class.
When running under .NET Memory Profiler, the LoadBitmap method will be replaced with the following method:
using System.Drawing; using SciTech.NetMemProfiler; Bitmap LoadBitmap() { Bitmap returnValue; // --- Generated by .NET Memory Profiler // Collect a reference snapshot MemSnapShot refSnapshot = MemProfiler.FastSnapshot(); try { // --- Original code using( LoadBitmapDialog dlg = new LoadBitmapDialog() ) { dlg.ShowDialog(); return dlg.LoadedBitmap; } // --- Generated by .NET Memory Profiler } finally { // Build the AssertionsDefinition, as // defined by the attributes. using( MemAssertion.BeginAssertions() ) { AssertionsDefinition ad = new AssertionsDefinition(); ad.NoNewInstances( “System.Windows.Forms.*”, true ); ad.NoNewInstances( “System. Drawing.*”, true ); ad.MaxNewInstances( typeof( Bitmap ), 1 ); MemAssertion.Assert( refSnapshot, ad ); } } } |
Inheritance Hierarchy
System..::..Object
System..::..Attribute
SciTech.NetMemProfiler..::..AssertionAttribute
SciTech.NetMemProfiler..::..AllowInstancesAttribute
SciTech.NetMemProfiler..::..AllowNewInstancesAttribute
SciTech.NetMemProfiler..::..MaxBytesAttribute
SciTech.NetMemProfiler..::..MaxInstancesAttribute
SciTech.NetMemProfiler..::..MaxNewBytesAttribute
SciTech.NetMemProfiler..::..MaxNewInstancesAttribute
SciTech.NetMemProfiler..::..NoInstancesAttribute
SciTech.NetMemProfiler..::..NoNewInstancesAttribute
System..::..Attribute
SciTech.NetMemProfiler..::..AssertionAttribute
SciTech.NetMemProfiler..::..AllowInstancesAttribute
SciTech.NetMemProfiler..::..AllowNewInstancesAttribute
SciTech.NetMemProfiler..::..MaxBytesAttribute
SciTech.NetMemProfiler..::..MaxInstancesAttribute
SciTech.NetMemProfiler..::..MaxNewBytesAttribute
SciTech.NetMemProfiler..::..MaxNewInstancesAttribute
SciTech.NetMemProfiler..::..NoInstancesAttribute
SciTech.NetMemProfiler..::..NoNewInstancesAttribute
Thread Safety
Static members of this type are safe for multi-threaded operations. Instance members of this type are safe for multi-threaded operations.