Class AssertionAttribute
Represents the base class for all memory assertion attributes.
Inheritance
Inherited Members
Namespace: SciTech.NetMemProfiler
Assembly: SciTech.MemProfilerApi.dll
Syntax
public abstract class AssertionAttribute : 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 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 );
}
}
}
Properties
Name | Description |
---|---|
IncludeSubclasses | Gets or sets a value indicating whether instances of all subclasses of the specified types should be checked when the target method has been executed. |
Type | Gets the Type of the instances that should be checked when the target method has been executed. |
TypeName | Gets the type name of the instances that should be checked when the target method has been executed. |
Methods
Name | Description |
---|---|
GetTypeNames() | Gets a string array containing the type names of the instances that should be checked when the target method has been executed. |
GetTypes() | Gets the Types of the instances that should be checked when the target method has been executed. |