共用方式為


PrivateObject Class

Allows test code to call methods and properties on the code under test that would be inaccessible because they are not public.

Inheritance Hierarchy

Object
  Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject

Namespace:  Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)

Syntax

'Declaration
Public Class PrivateObject
public class PrivateObject
public ref class PrivateObject
type PrivateObject =  class end
public class PrivateObject

The PrivateObject type exposes the following members.

Constructors

  Name Description
Public method PrivateObject(Object) Initializes a new instance of the PrivateObject class that creates the wrapper for the specified object.
Public method PrivateObject(Object, PrivateType) Initializes a new instance of the PrivateObject class that creates the wrapper for the specified object.
Public method PrivateObject(Object, String) Initializes a new instance of the PrivateObject class that creates the wrapper for the specified object.
Public method PrivateObject(Type, array<Object[]) Initializes a new instance of the PrivateObject class that creates the wrapper for the specified object.
Public method PrivateObject(String, String, array<Object[]) Initializes a new instance of the PrivateObject class. This creates the object of the specified type and wraps it in the private object.
Public method PrivateObject(Type, array<Type[], array<Object[]) Initializes a new instance of the PrivateObject class.
Public method PrivateObject(String, String, array<Type[], array<Object[]) Initializes a new instance of the PrivateObject class. This creates an object of the specified type and wraps it in the private object.

Top

Properties

  Name Description
Public property RealType Gets the type of the underlying object.
Public property Target Gets or sets the wrapped object.

Top

Methods

  Name Description
Public method Equals Returns a value that indicates whether two private objects are encapsulating the same target object. (Overrides Object.Equals(Object).)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetArrayElement(String, array<Int32[]) Gets the array element by using an array of subscripts for each dimension.
Public method GetArrayElement(String, BindingFlags, array<Int32[]) Gets the array element by using an array of subscripts for each dimension.
Public method GetField(String) Gets a value from a named field, based on the name.
Public method GetField(String, BindingFlags) Gets a value from a named field, based on the name and lookup mask.
Public method GetFieldOrProperty(String) Gets a value of a wrapped field or property based on the name.
Public method GetFieldOrProperty(String, BindingFlags) Gets a value of a wrapped field or property based on the name.
Public method GetHashCode Gets the hash code of the wrapped object. (Overrides Object.GetHashCode.)
Public method GetProperty(String, array<Object[]) Gets a property for a wrapped object identified by name.
Public method GetProperty(String, BindingFlags, array<Object[]) Gets a property for a wrapped object identified by name with binding flags.
Public method GetProperty(String, array<Type[], array<Object[]) Gets a property for a wrapped object identified by name.
Public method GetProperty(String, BindingFlags, array<Type[], array<Object[]) Gets an indexed property for a wrapped object identified by name.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Invoke(String, array<Object[]) Used to access the members of the private object.
Public method Invoke(String, array<Object[], CultureInfo) Used to access the members of the private object.
Public method Invoke(String, BindingFlags, array<Object[]) Used to access the members of the private object.
Public method Invoke(String, array<Type[], array<Object[]) Used to access the methods of the private object.
Public method Invoke(String, BindingFlags, array<Object[], CultureInfo) Used to access the members of the private object.
Public method Invoke(String, BindingFlags, array<Type[], array<Object[]) Used to access the members of the private object.
Public method Invoke(String, array<Type[], array<Object[], CultureInfo) Used to access the members of the private object.
Public method Invoke(String, array<Type[], array<Object[], array<Type[]) Used to access generic members of a private object.
Public method Invoke(String, BindingFlags, array<Type[], array<Object[], CultureInfo) Used to access the members of the private object.
Public method Invoke(String, BindingFlags, array<Type[], array<Object[], CultureInfo, array<Type[]) Used to access generic members of a private object.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SetArrayElement(String, Object, array<Int32[]) Sets the array element by using an array of subscripts for each dimension.
Public method SetArrayElement(String, BindingFlags, Object, array<Int32[]) Sets the array element by using an array of subscripts for each dimension.
Public method SetField(String, Object) Sets a value for the field of the wrapped object, identified by name.
Public method SetField(String, BindingFlags, Object) Sets a value for the field of the wrapped object, identified by name.
Public method SetFieldOrProperty(String, Object) Sets a value of a wrapped field or property based on the name.
Public method SetFieldOrProperty(String, BindingFlags, Object) Sets a value of a wrapped field or property based on the name.
Public method SetProperty(String, Object, array<Object[]) Sets a property for a wrapped object identified by name.
Public method SetProperty(String, BindingFlags, Object, array<Object[]) Sets a property for a wrapped object identified by name with binding flags.
Public method SetProperty(String, array<Type[], Object, array<Object[]) Sets an indexed property for a wrapped object identified by name.
Public method SetProperty(String, BindingFlags, Object, array<Type[], array<Object[]) Sets an indexed property for a wrapped object identified by name.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

The test generator creates most of the calls to these objects. The test generator creates code to wrap the private objects in a wrapper that allows access to the private object's members.

For a list of initial property values for an instance of the PrivateObject class, see the PrivateObject constructor.

This class represents the live non-public internal object in the system.

.NET Framework Security

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.VisualStudio.TestTools.UnitTesting Namespace

Other Resources

How to: Test a Private Method