Search Results for

    Show / Hide Table of Contents

    Class AssertionAttribute

    Represents the base class for all memory assertion attributes.

    Inheritance
    System.Object
    System.Attribute
    AssertionAttribute
    AllowInstancesAttribute
    AllowNewInstancesAttribute
    MaxAllocatedBytesAttribute
    MaxAllocationsAttribute
    MaxBytesAttribute
    MaxInstancesAttribute
    MaxNewBytesAttribute
    MaxNewInstancesAttribute
    NoInstancesAttribute
    NoNewInstancesAttribute
    Inherited Members
    System.Attribute.Equals(System.Object)
    System.Attribute.GetHashCode()
    System.Object.Equals(System.Object, System.Object)
    System.Object.GetType()
    System.Object.MemberwiseClone()
    System.Object.ReferenceEquals(System.Object, System.Object)
    System.Object.ToString()
    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:

    Attribute classCorresponding methods in the AssertionsDefinition class
    NoInstancesAttribute AssertionsDefinition.NoInstances
    NoNewInstancesAttribute AssertionsDefinition.NoNewInstances
    AllowInstancesAttribute AssertionsDefinition.AllowInstances
    AllowNewInstancesAttribute AssertionsDefinition.AllowNewInstances
    MaxInstancesAttribute AssertionsDefinition.MaxInstances
    MaxNewInstancesAttribute AssertionsDefinition.MaxNewInstances
    MaxBytesAttribute AssertionsDefinition.MaxBytes
    MaxNewBytesAttribute AssertionsDefinition.MaxNewBytes
    MaxAllocationsAttribute AssertionsDefinition.MaxAllocations
    MaxAllocatedBytesAttribute AssertionsDefinition.MaxAllocatedBytes
    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.

    In This Article
    Back to top

    © Copyright 2002-2020. SciTech Software AB.
    For information about .NET Memory Profiler, see the product site at https://memprofiler.com