skip navigation
  • Product Bundles

    DevCraft

    All Telerik .NET tools and Kendo UI JavaScript components in one package. Now enhanced with:

    • NEW: Design Kits for Figma
    • Online Training
    • Document Processing Library
    • Embedded Reporting for web and desktop

    Web

    Kendo UI UI for Angular UI for Vue UI for jQuery KendoReact UI for Blazor UI for ASP.NET Core UI for ASP.NET MVC UI for ASP.NET AJAX

    Mobile

    UI for .NET MAUI

    Document Management

    Telerik Document Processing

    Desktop

    UI for .NET MAUI UI for WinUI UI for WinForms UI for WPF

    Reporting

    Telerik Reporting Telerik Report Server

    Testing & Mocking

    Test Studio Telerik JustMock

    CMS

    Sitefinity

    AI Productivity Tools

    AI Coding Assistants

    UI/UX Tools

    ThemeBuilder Design System Kit Templates and Building Blocks

    Debugging

    Fiddler Fiddler Everywhere Fiddler Classic Fiddler Everywhere Reporter FiddlerCore

    Free Tools

    KendoReact Free VB.NET to C# Converter Testing Framework
    View all products
  • Overview
    • What's New
    • Roadmap
    • Release History
  • Docs & Support
  • Pricing
  • Shopping cart
    • Account Overview
    • Your Licenses
    • Downloads
    • Support Center
    • Forum Profile
    • Payment Methods
    • Edit Profile
    • Log out
  • Login
  • Contact Us
  • Try now

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
System.Object
PrivateAccessor
Inherited Members
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
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

Extension Methods

FluentHelper.Arrange<T, TResult>(T, Expression<Func<T, TResult>>)
FluentHelper.Arrange<T>(T, Expression<Action<T>>)
FluentHelper.ArrangeSet<T>(T, Action<T>)
FluentHelper.ArrangeLike<T>(T, Expression<Func<T, Boolean>>)
FluentHelper.Assert<T, TReturn>(T, Expression<Func<T, TReturn>>, String)
FluentHelper.Assert<T, TReturn>(T, Expression<Func<T, TReturn>>, Occurs, String)
FluentHelper.Assert<T>(T, Expression<Action<T>>, String)
FluentHelper.Assert<T>(T, Expression<Action<T>>, Occurs, String)
FluentHelper.AssertSet<T>(T, Action<T>, String)
FluentHelper.AssertSet<T>(T, Action<T>, Occurs, String)
FluentHelper.Raise<T>(T, Action<T>, EventArgs)
FluentHelper.Raise<T>(T, Action<T>, Object[])
FluentHelper.Assert<T>(T, String)
FluentHelper.AssertAll<T>(T, String)
Getting Started
  • Unit Testing and Mocking Explained
  • Videos
  • FAQs
Community
  • Forums
  • Blogs
  • Feedback Portal

Copyright © 2019 Progress Software Corporation and/or its subsidiaries or affiliates.
All Rights Reserved.

Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks for appropriate markings.