PrivateObjectExtensions 1.4.0

dotnet add package PrivateObjectExtensions --version 1.4.0                
NuGet\Install-Package PrivateObjectExtensions -Version 1.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PrivateObjectExtensions" Version="1.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PrivateObjectExtensions --version 1.4.0                
#r "nuget: PrivateObjectExtensions, 1.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PrivateObjectExtensions as a Cake Addin
#addin nuget:?package=PrivateObjectExtensions&version=1.4.0

// Install PrivateObjectExtensions as a Cake Tool
#tool nuget:?package=PrivateObjectExtensions&version=1.4.0                

PrivateObjectExtensions

NuGet

PrivateObjectExtensions provides extension methods of PrivateObject and PrivateType for unit test projects. This allows you to

  • get/set private (and any other) fields/properties by simple extension methods,
  • even if the member is declared in base type, or
  • even if the property is getter only.

Requirements

No dependencies. You can use this library for any projects, but I recommend to use only in unit test projects.

Supported platform:

  • .NET Framework 4.0+
  • .NET Core 2.0+

PrivateObject/PrivateType codes are copied from TestFX for removing dependencies. The classes are not included in .NET Core implementation but that's why you can use this package on .NET Core.

Sample

public class Base
{
    private string _private = "private member";
    private static string _privateStatic = "private static member";
}

public class Derived : Base
{
}
[TestMethod]
public void GetMembers()
{
    var derived = new Derived();
    var value1 = derived.GetPrivate("_private");
    var value2 = derived.GetPrivate<string>("_private");
    var value3 = derived.GetPrivate("_privateStatic");
    var value4 = typeof(Base).GetPrivate("_privateStatic");
    // ...
}

[TestMethod]
public void SetMembers()
{
    var derived = new Derived();
    derived.SetPrivate("_private", "changed");
    derived.SetPrivate("_privateStatic", "changed");
    typeof(Base).SetPrivate("_privateStatic", "changed");
    // ...
}
  • GetPrivate() is a wrapper of PrivateObject.GetFieldOrProperty() and PrivateType.GetStaticFieldOrProperty().
  • SetPrivate() is a wrapper of PrivateObject.SetFieldOrProperty() and PrivateType.SetStaticFieldOrProperty().

These extension methods are in namespace of System and extends object type. See more samples in Test projects.

Why PrivateObjectExtensions?

PrivateObject doesn't allow you to access if the member is declared in base type unless you specify that type.

// without PrivateObjectExtensions
var targetType = // find the type declaring the member somehow
var po = new PrivateObject(yourObject, new PrivateType(targetType));
po.GetField("_private");

Additionally, if you want to access static member, you have to use different way.

// without PrivateObjectExtensions
var targetType = // find the type declaring the member somehow
var pt = new PrivateType(targetType);
pt.GetStaticField("_privateStatic");

These are totally useless works. What we want to do is just accessing private member simply regardless of it's real type or static. PrivateObjectExtensions automatically find the way to access the member. No need to concern about them all!

This is useful especially when you are mocking by inheriting, for instance using Moq. Moq mocks an object by inheriting the type. Once we create an instance as a mock object, we have to access private member as above. However, you can simply access by using PrivateObjectExtensions.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on PrivateObjectExtensions:

Repository Stars
pnp/pnpframework
PnP Framework is a .NET library targeting Microsoft 365 containing the PnP Provisioning engine and a ton of other useful extensions
Version Downloads Last updated
1.4.0 200,174 6/1/2019
1.3.0 592 5/30/2019
1.2.0 613 5/27/2019
1.1.0 579 5/25/2019