Class
PrivateAccessor

Gives access to the non-public members of a type or instance. This class provides functionality similar to the one that exists in regular reflection classes with the added benefit that it 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)

Creates a new PrivateAccessor wrapping the given instance. Can be used to access both instance and static members.

Declaration

cs-api-definition
public PrivateAccessor(object instance)

Parameters

instance

object

The instance to wrap.

Properties

Instance

The wrapped instance.

Declaration

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

Property Value

object

RefReturn

Non public ref return interface for mocking.

Declaration

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

Property Value

IPrivateRefReturnAccessor

RethrowOriginalOnCallMethod

Controls whether TargetInvocationException will be thrown, this is the default behavior, or the original exception when an exception occur on using one of the CallMethod overloads.

Declaration

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

Property Value

bool

Methods

CallMethod(MethodBase, params object[])

Calls the specified method.

Declaration

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

Parameters

method

MethodBase

The 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

The name of the method to call.

argTypes

ICollection<Type>

The method argument types.

argModifiers

ParameterModifier

The argument type modifiers, such as out and ref.

argValues

object[]

The argument values to pass to the method.

Returns

object

The value returned by the specified method.

CallMethod(string, ICollection<Type>, 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, object[] argValues)

Parameters

name

string

The name of the method to call.

argTypes

ICollection<Type>

The method argument types.

argValues

object[]

The argument values to pass to the method.

Returns

object

The value returned by the specified 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

The 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

The name of the method to call.

typeArguments

ICollection<Type>

The 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 'false', the static constructor will not be called if it has already run as part of type initialization. When 'true', the static constructor will be called unconditionally. If the type is not yet initialized and 'true' is given, the static constructor will be 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

Targeted type.

Returns

PrivateAccessor

PrivateAccessor type.

GetField(string)

Gets the value of a field.

Declaration

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

Parameters

name

string

The name of the field.

Returns

object

The value of the field

GetIndex(object)

Gets the value returned by the indexer (

this[T index]
in C#) for the specified index value.

Declaration

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

Parameters

index

object

The index argument to pass to the indexer.

Returns

object

The value returned by the indexer.

GetMember(string)

Gets the value of a field or property.

Declaration

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

Parameters

name

string

Name of a field or property to get.

Returns

object

The value of the field or property.

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

The name of the property.

indexArgs

object[]

Optional index arguments if the property has any.

Returns

object

The value of the property.

RaiseEvent(string, params object[])

Raises the specified event passing the given arguments to the 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 the event handlers. Must match the event handler signature exactly.

SetField(string, object)

Sets the value of a field.

Declaration

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

Parameters

name

string

The name of the field.

value

object

The new value of the field.

SetIndex(object, object)

Sets a value to the indexer (

this[T index]
in C#) for the specified index value.

Declaration

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

Parameters

index

object

The index argument to pass to the indexer.

value

object

The value to give to the indexer.

SetMember(string, object)

Sets the value of a field or property.

Declaration

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

Parameters

name

string

The name of a field or property to set

value

object

The new value of the field or property.

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

The name of the property.

value

object

The value to set to the property.

indexArgs

object[]

Optional index arguments if the property has any.

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