New to Telerik JustMockStart a free 30-day trial

Gives access to the non-public members of a type or instance. Unlike standard reflection, this class can bypass essential security checks related to accessing non-public members through reflection.

Definition

Namespace:Telerik.JustMock

Assembly:Telerik.JustMock.dll

Syntax:

C#
public sealed class PrivateAccessor : IDynamicMetaObjectProvider

Inheritance: objectPrivateAccessor

Implements: IDynamicMetaObjectProvider

Constructors

Initializes a new PrivateAccessor that wraps the given object instance, enabling access to both its instance and static non-public members.

C#
public PrivateAccessor(object instance)
Parameters:instanceobject

The object whose non-public members you want to access.

Properties

Gets the object instance wrapped by this PrivateAccessor. Returns null when this accessor was created via ForType(Type) for static member access.

C#
public object Instance { get; }
Property Value:

The wrapped instance, or null for a type-only (static) accessor.

Gets an IPrivateRefReturnAccessor that provides access to non-public members with ref return values on the wrapped instance or type.

C#
public IPrivateRefReturnAccessor RefReturn { get; }
Property Value:

An IPrivateRefReturnAccessor scoped to the same instance and type as this accessor.

Remarks:

This property is only available when the JustMock profiler is attached, as intercepting ref-returning members requires elevated (non-portable) mode.

Gets or sets a value that controls whether CallMethod(string, params object[]) and other CallMethod* overloads rethrow the original exception.

C#
public bool RethrowOriginalOnCallMethod { get; set; }
Remarks:

By default, reflection wraps exceptions in TargetInvocationException. Set this property to true if you want to receive the original exception instead when invoking methods through CallMethod(string, params object[]) or its overloads.

Methods

Calls the specified method.

C#
public object CallMethod(MethodBase method, params object[] args)
Parameters:methodMethodBase

Method to call.

argsobject[]

Arguments to pass to the method.

Returns:

object

Return value of the method.

Calls a non-public method by name and explicit parameter types.

C#
public object CallMethod(string name, ICollection<Type> argTypes, object[] argValues)
Parameters:namestring

Name of the method to call.

argTypesICollection<Type>

Method parameter types.

argValuesobject[]

Argument values to pass to the method.

Returns:

object

The value returned by the method.

Calls the specified method by name and given argument types and values.

C#
public object CallMethod(string name, ICollection<Type> argTypes, ParameterModifier argModifiers, object[] argValues)
Parameters:namestring

Name of the method to call.

argTypesICollection<Type>

Method parameter types.

argModifiersParameterModifier

Parameter modifiers such as ref and out.

argValuesobject[]

Argument values to pass to the method.

Returns:

object

The value returned by the method.

Calls the specified method by name.

C#
public object CallMethod(string name, params object[] args)
Parameters:namestring

Name of the method to call.

argsobject[]

Arguments to pass to the method.

Returns:

object

The value returned by the specified method.

Calls the specified generic method by name.

C#
public object CallMethodWithTypeArguments(string name, ICollection<Type> typeArguments, params object[] args)
Parameters:namestring

Name of the method to call.

typeArgumentsICollection<Type>

Type arguments to specialize the generic method.

argsobject[]

Arguments to pass to the method.

Returns:

object

The value returned by the specified method.

Calls the type's static constructor. The static constructor can be executed even when the runtime has already called it as part of type's initialization.

C#
public void CallStaticConstructor(bool forceCall)
Parameters:forceCallbool

When this value is false, JustMock does not rerun a static constructor that has already executed. When this value is true, JustMock calls the static constructor unconditionally. If the type is not initialized yet, the static constructor can run twice.

Creates a new PrivateAccessor wrapping the given type. Can be used to access the static members of a type.

C#
public static PrivateAccessor ForType(Type type)
Parameters:typeType

The type whose static non-public members you want to access. Must not be null.

Returns:

PrivateAccessor

A PrivateAccessor targeting the static members of type.

Exceptions:

ArgumentNullException

Thrown when type is null.

Gets the value of a non-public field by name on the wrapped instance or type.

C#
public object GetField(string name)
Parameters:namestring

The exact name of the field to read.

Returns:

object

The current value of the field, or null if the field holds a null reference.

Gets the value returned by the indexer for the specified index.

C#
public object GetIndex(object index)
Parameters:indexobject

Indexer argument.

Returns:

object

The indexer value.

Gets the value of a non-public field or property by name. Fields are resolved first; if no matching field is found, the name is treated as a property.

C#
public object GetMember(string name)
Parameters:namestring

The name of the field or property to read.

Returns:

object

The current value of the field or property, or null if the member holds a null reference.

Gets the value of a property by name.

C#
public object GetProperty(string name, params object[] indexArgs)
Parameters:namestring

Property name.

indexArgsobject[]

Optional index arguments for indexed properties.

Returns:

object

The property value.

Raises the specified event on the wrapped instance or type, invoking all currently subscribed handlers.

C#
public void RaiseEvent(string name, params object[] eventArgs)
Parameters:namestring

The name of the event to raise.

eventArgsobject[]

Arguments to pass to each event handler. The values must match the event delegate's parameter list exactly (e.g., sender followed by the EventArgs-derived argument).

Sets the value of a non-public field by name on the wrapped instance or type.

C#
public void SetField(string name, object value)
Parameters:namestring

The exact name of the field to write.

valueobject

The value to assign to the field. Must be assignable to the field's declared type, or null for reference and nullable types.

Sets the value of the indexer for the specified index.

C#
public void SetIndex(object index, object value)
Parameters:indexobject

Indexer argument.

valueobject

The value to give to the indexer.

Sets the value of a non-public field or property by name. Fields are resolved first; if no matching field is found, the name is treated as a property.

C#
public void SetMember(string name, object value)
Parameters:namestring

The name of the field or property to write.

valueobject

The value to assign to the field or property.

Exceptions:

MissingMemberException

Thrown when neither a field nor a property with the given name exists on the target type.

ArgumentException

Thrown when value is not assignable to the member's declared type.

Sets the value of a property by name.

C#
public void SetProperty(string name, object value, params object[] indexArgs)
Parameters:namestring

Property name.

valueobject

The value to set to the property.

indexArgsobject[]

Optional index arguments for indexed properties.

Gets the value of a dynamic private accessor expression. Use this when the value to get is of type Object, otherwise cast the expression to the desired type.

C#
public static object Unwrap(dynamic privateAccessor)
Parameters:privateAccessordynamic

A PrivateAccessor expression built from a dynamic variable.

Returns:

object

The value of the private accessor expression