Class
PrivateAccessor

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:

cs-api-definition
public sealed class PrivateAccessor : IDynamicMetaObjectProvider

Inheritance: objectPrivateAccessor

Implements: IDynamicMetaObjectProvider

Constructors

PrivateAccessor(object)

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

Declaration

cs-api-definition
public PrivateAccessor(object instance)

Parameters

instance

object

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

Properties

Instance

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

Declaration

cs-api-definition
public object Instance { get; }

Property Value

object

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

RefReturn

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

Declaration

cs-api-definition
public IPrivateRefReturnAccessor RefReturn { get; }

Property Value

IPrivateRefReturnAccessor

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.

RethrowOriginalOnCallMethod

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

Declaration

cs-api-definition
public bool RethrowOriginalOnCallMethod { get; set; }

Property Value

bool

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

CallMethod(MethodBase, params object[])

Calls the specified method.

Declaration

cs-api-definition
public object CallMethod(MethodBase method, params object[] args)

Parameters

method

MethodBase

Method to call.

args

object[]

Arguments to pass to the method.

Returns

object

Return value of the method.

CallMethod(string, ICollection<Type>, ParameterModifier, object[])

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

Declaration

cs-api-definition
public object CallMethod(string name, ICollection<Type> argTypes, ParameterModifier argModifiers, object[] argValues)

Parameters

name

string

Name of the method to call.

argTypes

ICollection<Type>

Method parameter types.

argModifiers

ParameterModifier

Parameter modifiers such as ref and out.

argValues

object[]

Argument values to pass to the method.

Returns

object

The value returned by the method.

CallMethod(string, ICollection<Type>, object[])

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

Declaration

cs-api-definition
public object CallMethod(string name, ICollection<Type> argTypes, object[] argValues)

Parameters

name

string

Name of the method to call.

argTypes

ICollection<Type>

Method parameter types.

argValues

object[]

Argument values to pass to the method.

Returns

object

The value returned by the method.

CallMethod(string, params object[])

Calls the specified method by name.

Declaration

cs-api-definition
public object CallMethod(string name, params object[] args)

Parameters

name

string

Name of the method to call.

args

object[]

Arguments to pass to the method.

Returns

object

The value returned by the specified method.

CallMethodWithTypeArguments(string, ICollection<Type>, params object[])

Calls the specified generic method by name.

Declaration

cs-api-definition
public object CallMethodWithTypeArguments(string name, ICollection<Type> typeArguments, params object[] args)

Parameters

name

string

Name of the method to call.

typeArguments

ICollection<Type>

Type arguments to specialize the generic method.

args

object[]

Arguments to pass to the method.

Returns

object

The value returned by the specified method.

CallStaticConstructor(bool)

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.

Declaration

cs-api-definition
public void CallStaticConstructor(bool forceCall)

Parameters

forceCall

bool

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.

ForType(Type)

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

Declaration

cs-api-definition
public static PrivateAccessor ForType(Type type)

Parameters

type

Type

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.

GetField(string)

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

Declaration

cs-api-definition
public object GetField(string name)

Parameters

name

string

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.

GetIndex(object)

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

Declaration

cs-api-definition
public object GetIndex(object index)

Parameters

index

object

Indexer argument.

Returns

object

The indexer value.

GetMember(string)

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.

Declaration

cs-api-definition
public object GetMember(string name)

Parameters

name

string

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.

GetProperty(string, params object[])

Gets the value of a property by name.

Declaration

cs-api-definition
public object GetProperty(string name, params object[] indexArgs)

Parameters

name

string

Property name.

indexArgs

object[]

Optional index arguments for indexed properties.

Returns

object

The property value.

RaiseEvent(string, params object[])

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

Declaration

cs-api-definition
public void RaiseEvent(string name, params object[] eventArgs)

Parameters

name

string

The name of the event to raise.

eventArgs

object[]

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).

SetField(string, object)

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

Declaration

cs-api-definition
public void SetField(string name, object value)

Parameters

name

string

The exact name of the field to write.

value

object

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

SetIndex(object, object)

Sets the value of the indexer for the specified index.

Declaration

cs-api-definition
public void SetIndex(object index, object value)

Parameters

index

object

Indexer argument.

value

object

The value to give to the indexer.

SetMember(string, object)

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.

Declaration

cs-api-definition
public void SetMember(string name, object value)

Parameters

name

string

The name of the field or property to write.

value

object

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.

SetProperty(string, object, params object[])

Sets the value of a property by name.

Declaration

cs-api-definition
public void SetProperty(string name, object value, params object[] indexArgs)

Parameters

name

string

Property name.

value

object

The value to set to the property.

indexArgs

object[]

Optional index arguments for indexed properties.

Unwrap(dynamic)

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.

Declaration

cs-api-definition
public static object Unwrap(dynamic privateAccessor)

Parameters

privateAccessor

dynamic

A PrivateAccessor expression built from a dynamic variable.

Returns

object

The value of the private accessor expression