PrivateObjectExtensions 1.4.0

Extension methods for accessing private fields / properties for unit test projects.

     Supported platform:
     - .NET Framework 4.0+
     - .NET Core 2.0+

Install-Package PrivateObjectExtensions -Version 1.4.0
dotnet add package PrivateObjectExtensions --version 1.4.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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+

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.

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+

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.

Dependencies

This package has no dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.4.0 319 6/1/2019
1.3.0 91 5/30/2019
1.2.0 87 5/27/2019
1.1.0 79 5/25/2019