Coder Social home page Coder Social logo

exposedobject's Introduction

ExposedObject

Fast dynamic wrapper for accessing hidden methods and fields of .Net objects. Uses 4.0 dynamic feature to allow seamless access to non-public object members. Facilitates white-box unit testing, exposes APIs that should be public and allows construction of elaborate hacks. Should not kill your cat provided you are careful. Available on NuGet.

Usage examples

Handling private instance methods, fields and properties on a visible type:

// create an Exposed instance from a ClassWithHiddenMethods instance
dynamic exposed = Exposed.From(new ClassWithHiddenMethods());
// calling a private method
string password = exposed.GeneratePassword(8);
// reading a private field
int privateFieldValue = exposed.internalCount;
// setting a private field
exposed.internalCount = privateFieldValue * 2;
// reading a protected property
char protectedPropertyValue = exposed.InternalData;

Accessing private field on a hidden type:

// get the Type via reflection
Type hiddenClass = Type.GetType("TestSubjects.HiddenClass, TestSubjects");
// call the parameterless constructor of a hidden type
dynamic exposed = Exposed.New(hiddenClass);
string password = exposed.password;

Accessing internal static method from type:

dynamic exposed = Exposed.From(typeof(ClassWithInternalStaticMethod));
decimal convertValue = exposed.ConvertValue(8);

TODO:

  • performance assessment (binding restrictions vs caching, compare with CreateDelegate solution)
  • generic methods (there are tests for them, some suggested solutions on StackOverflow )
  • constructors (currently Exposed.New() delegates to Activator.CreateInstance() )

Ideas borrowed from:

exposedobject's People

Contributors

skolima avatar

Watchers

James Cloos avatar Houfeng avatar  avatar

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.