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:
public sealed class PrivateAccessor : IDynamicMetaObjectProvider
Inheritance: objectPrivateAccessor
Implements:
Constructors
Initializes a new PrivateAccessor that wraps the given object instance, enabling access to both its instance and static non-public members.
public PrivateAccessor(object instance)
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.
public object Instance { get; }
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.
public IPrivateRefReturnAccessor RefReturn { get; }
An IPrivateRefReturnAccessor scoped to the same instance and type as this accessor.
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.
public bool RethrowOriginalOnCallMethod { get; set; }
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.
public object CallMethod(MethodBase method, params object[] args)
Method to call.
argsobject[]Arguments to pass to the method.
Returns:Return value of the method.
Calls a non-public method by name and explicit parameter types.
public object CallMethod(string name, ICollection<Type> argTypes, object[] argValues)
Name of the method to call.
argTypesICollection<Type>Method parameter types.
argValuesobject[]Argument values to pass to the method.
Returns:The value returned by the method.
Calls the specified method by name and given argument types and values.
public object CallMethod(string name, ICollection<Type> argTypes, ParameterModifier argModifiers, object[] argValues)
Name of the method to call.
argTypesICollection<Type>Method parameter types.
argModifiersParameterModifierParameter modifiers such as ref and out.
Argument values to pass to the method.
Returns:The value returned by the method.
Calls the specified generic method by name.
public object CallMethodWithTypeArguments(string name, ICollection<Type> typeArguments, params object[] args)
Name of the method to call.
typeArgumentsICollection<Type>Type arguments to specialize the generic method.
argsobject[]Arguments to pass to the method.
Returns: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.
public void CallStaticConstructor(bool 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.
Creates a new PrivateAccessor wrapping the given type. Can be used to access the static members of a type.
public static PrivateAccessor ForType(Type 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.
Thrown when type is null.
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.
Raises the specified event on the wrapped instance or type, invoking all currently subscribed handlers.
public void RaiseEvent(string name, params object[] eventArgs)
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.
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.
public void SetMember(string name, object value)
The name of the field or property to write.
valueobjectThe 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.
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.
public static object Unwrap(dynamic privateAccessor)
A PrivateAccessor expression built from a dynamic variable.
Returns:The value of the private accessor expression