GithubHelp home page GithubHelp logo

privateobjectextensions's Introduction

PrivateObjectExtensions

NuGet

PrivateObjectExtensions provides extension methods for object type so that you can easily:

  • 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.

Originally this package is a wrapper of PrivateObject and PrivateType of .NET Framework but they are no longer available in .NET Core or later. Now the classes are copied and included in this package and has no dependencies.

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's People

Contributors

cactuaroid avatar

Stargazers

 avatar F_Unction avatar  avatar  avatar Chris Shim avatar Serhii Kalinets avatar Alex Stek avatar Zhiwen Lin avatar Eli Belash avatar

Watchers

James Cloos avatar  avatar

Forkers

isalo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.