ObjectState Class

Typemock Isolator Developer Guide
Utility Class that takes a snapshot of all the fields of an object or the static fields of a type , in order to reset all the fields back to the saved state.
Inheritance Hierarchy

SystemObject
  TypeMockObjectState

Namespace:  TypeMock
Assembly:  TypeMock (in TypeMock.dll) Version: 9.3.6.0 (9.3.6.0)
Syntax

public class ObjectState

The ObjectState type exposes the following members.

Constructors

  NameDescription
Public methodObjectState(Object)
Take a snapshot of all the fields of an object, in order to reset all the fields back to the saved state.
Public methodObjectState(Type)
Take a snapshot of all the static fields of a type, in order to reset all the fields back to the saved state.
Top
Methods

  NameDescription
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetField(String)
Gets the value of a non-public field (member variable) of the managed type.
Public methodStatic memberGetField(Object, String)
Gets the value of a non-public field (member variable) of an instance.
Public methodStatic memberGetField(Type, String)
Gets the value of a non-public static field (member variable) of a type.
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodResetState
Reset the fields back to their original state
Public methodSetField(String, Object)
Sets the value of a non-public field (member variable) of the managed type.
Public methodStatic memberSetField(Object, String, Object)
Sets the value of a non-public field (member variable) of an instance.
Public methodStatic memberSetField(Type, String, Object)
Sets the value of a non-public static field (member variable) of a type.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Remarks

The ObjectState assists testing classes with hidden field, by resetting these field when required
Examples

The following example we want to test a class that has a static counter.
public class StatefulClass
{
    static private int count = 0;
    public int Count {get { return count++ ;}}
}

[Test]
public void Stateful()
{
    StatefulClass stateful = new StatefulClass();
    // Save our state
    ObjectState objState = new ObjectState(state);
    // Do something to change the state
       Assert.AreEqual(0,stateful.Count);
       // lets reset our code for next test
       objState.ResetState();
       Assert.AreEqual(0,stateful.Count);
   }
See Also

Reference