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.
Inheritance
Inherited Members
Namespace: Telerik.JustMock
Assembly: Telerik.JustMock.dll
Syntax
public sealed class PrivateAccessor : IDynamicMetaObjectProvider
Remarks
When the profiler is enabled, PrivateAccessor acquires additional power:
- It can even be used to access all kinds of non-public members in Silverlight (and when running in partial trust in general).
- All calls made through PrivateAccessor are always made with full trust (unrestricted) permissions.
You can assign a PrivateAccessor to a dynamic variable and access it as if you're referencing the original object: dynamic acc = new PrivateAccessor(myobj); acc.PrivateProperty = acc.PrivateMethod(123); // PrivateProperty and PrivateMethod are private members on myobj's type.
Constructors
PrivateAccessor(Object)
Creates a new PrivateAccessor wrapping the given instance. Can be used to access both instance and static members.
Declaration
public PrivateAccessor(object instance)
Parameters
System.Object
instance
The instance to wrap. |
Properties
Instance
The wrapped instance.
Declaration
public object Instance { get; }
Property Value
System.Object
|
RefReturn
Non public ref return interface for mocking.
Declaration
public IPrivateRefReturnAccessor RefReturn { get; }
Property Value
IPrivateRefReturnAccessor
|
RethrowOriginalOnCallMethod
Controls whether System.Reflection.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
public bool RethrowOriginalOnCallMethod { get; set; }
Property Value
System.Boolean
|
Methods
CallMethod(MethodBase, Object[])
Calls the specified method.
Declaration
public object CallMethod(MethodBase method, params object[] args)
Parameters
System.Reflection.MethodBase
method
The method to call. |
System.Object[]
args
Arguments to pass to the method. |
Returns
System.Object
Return value of the method. |
CallMethod(String, ICollection<Type>, Object[])
Calls the specified method by name and given argument types and values.
Declaration
public object CallMethod(string name, ICollection<Type> argTypes, object[] argValues)
Parameters
System.String
name
The name of the method to call. |
System.Collections.Generic.ICollection<System.Type>
argTypes
The method argument types. |
System.Object[]
argValues
The argument values to pass to the method. |
Returns
System.Object
The value returned by the specified 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
System.String
name
The name of the method to call. |
System.Collections.Generic.ICollection<System.Type>
argTypes
The method argument types. |
System.Reflection.ParameterModifier
argModifiers
The argument type modifiers, such as out and ref. |
System.Object[]
argValues
The argument values to pass to the method. |
Returns
System.Object
The value returned by the specified method. |
CallMethod(String, Object[])
Calls the specified method by name.
Declaration
public object CallMethod(string name, params object[] args)
Parameters
System.String
name
The name of the method to call. |
System.Object[]
args
Arguments to pass to the method. |
Returns
System.Object
The value returned by the specified method. |
CallMethodWithTypeArguments(String, ICollection<Type>, Object[])
Calls the specified generic method by name.
Declaration
public object CallMethodWithTypeArguments(string name, ICollection<Type> typeArguments, params object[] args)
Parameters
System.String
name
The name of the method to call. |
System.Collections.Generic.ICollection<System.Type>
typeArguments
The type arguments to specialize the generic method. |
System.Object[]
args
Arguments to pass to the method. |
Returns
System.Object
The value returned by the specified method. |
CallStaticConstructor(Boolean)
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
System.Boolean
forceCall
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
public static PrivateAccessor ForType(Type type)
Parameters
System.Type
type
Targeted type. |
Returns
PrivateAccessor
PrivateAccessor type. |
GetField(String)
Gets the value of a field.
Declaration
public object GetField(string name)
Parameters
System.String
name
The name of the field. |
Returns
System.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
public object GetIndex(object index)
Parameters
System.Object
index
The index argument to pass to the indexer. |
Returns
System.Object
The value returned by the indexer. |
GetMember(String)
Gets the value of a field or property.
Declaration
public object GetMember(string name)
Parameters
System.String
name
Name of a field or property to get. |
Returns
System.Object
The value of the field or property. |
GetProperty(String, Object[])
Gets the value of a property by name.
Declaration
public object GetProperty(string name, params object[] indexArgs)
Parameters
System.String
name
The name of the property. |
System.Object[]
indexArgs
Optional index arguments if the property has any. |
Returns
System.Object
The value of the property. |
RaiseEvent(String, Object[])
Raises the specified event passing the given arguments to the handlers.
Declaration
public void RaiseEvent(string name, params object[] eventArgs)
Parameters
System.String
name
The name of the event to raise. |
System.Object[]
eventArgs
Arguments to pass to the event handlers. Must match the event handler signature exactly. |
SetField(String, Object)
Sets the value of a field.
Declaration
public void SetField(string name, object value)
Parameters
System.String
name
The name of the field. |
System.Object
value
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
public void SetIndex(object index, object value)
Parameters
System.Object
index
The index argument to pass to the indexer. |
System.Object
value
The value to give to the indexer. |
SetMember(String, Object)
Sets the value of a field or property.
Declaration
public void SetMember(string name, object value)
Parameters
System.String
name
The name of a field or property to set |
System.Object
value
The new value of the field or property. |
SetProperty(String, Object, Object[])
Sets the value of a property by name.
Declaration
public void SetProperty(string name, object value, params object[] indexArgs)
Parameters
System.String
name
The name of the property. |
System.Object
value
The value to set to the property. |
System.Object[]
indexArgs
Optional index arguments if the property has any. |
Unwrap(Object)
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
System.Object
privateAccessor
A PrivateAccessor expression built from a dynamic variable. |
Returns
System.Object
The value of the private accessor expression |