ClassPrivateAccessor
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:
public sealed class PrivateAccessor : IDynamicMetaObjectProvider
Inheritance: objectPrivateAccessor
Implements:
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
public PrivateAccessor(object instance)
Parameters
instance
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.
RefReturn
Gets an IPrivateRefReturnAccessor that provides access to non-public members with ref return values on the wrapped instance or type.
Declaration
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.
RethrowOriginalOnCallMethod
Gets or sets a value that controls whether CallMethod(string, params object[]) and other CallMethod* overloads rethrow the original exception.
Declaration
public bool RethrowOriginalOnCallMethod { get; set; }
Property Value
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
public object CallMethod(MethodBase method, params object[] args)
Parameters
method
Method to call.
args
object[]
Arguments to pass to the method.
Returns
Return value of the method.
CallMethod(string, ICollection<Type>, ParameterModifier, object[])
Calls the specified method by name and given argument types and values.
Declaration
public object CallMethod(string name, ICollection<Type> argTypes, ParameterModifier argModifiers, object[] argValues)
Parameters
name
Name of the method to call.
argTypes
Method parameter types.
argModifiers
Parameter modifiers such as ref and out.
argValues
object[]
Argument values to pass to the method.
Returns
The value returned by the method.
CallMethod(string, ICollection<Type>, object[])
Calls a non-public method by name and explicit parameter types.
Declaration
public object CallMethod(string name, ICollection<Type> argTypes, object[] argValues)
Parameters
name
Name of the method to call.
argTypes
Method parameter types.
argValues
object[]
Argument values to pass to the method.
Returns
The value returned by the method.
CallMethod(string, params object[])
Calls the specified method by name.
CallMethodWithTypeArguments(string, ICollection<Type>, params object[])
Calls the specified generic method by name.
Declaration
public object CallMethodWithTypeArguments(string name, ICollection<Type> typeArguments, params object[] args)
Parameters
name
Name of the method to call.
typeArguments
Type arguments to specialize the generic method.
args
object[]
Arguments to pass to the method.
Returns
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
public void CallStaticConstructor(bool forceCall)
Parameters
forceCall
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
public static PrivateAccessor ForType(Type type)
Parameters
type
The type whose static non-public members you want to access. Must not be null.
Returns
A PrivateAccessor targeting the static members of type.
Exceptions
Thrown when type is null.
GetField(string)
Gets the value of a non-public field by name on the wrapped instance or type.
GetIndex(object)
Gets the value returned by the indexer for the specified index.
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.
GetProperty(string, params object[])
Gets the value of a property by name.
RaiseEvent(string, params object[])
Raises the specified event on the wrapped instance or type, invoking all currently subscribed handlers.
Declaration
public void RaiseEvent(string name, params object[] eventArgs)
Parameters
name
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.
SetIndex(object, object)
Sets the value of the indexer for the specified index.
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
public void SetMember(string name, object value)
Parameters
name
The name of the field or property to write.
value
The value to assign to the field or property.
Exceptions
Thrown when neither a field nor a property with the given name exists on the target type.
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.
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
public static object Unwrap(dynamic privateAccessor)
Parameters
privateAccessor
dynamic
A PrivateAccessor expression built from a dynamic variable.
Returns
The value of the private accessor expression