Skip to content

MahApps/ExposedObject

This branch is 45 commits behind skolima/ExposedObject:main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

5b04a18 · Jun 30, 2020

History

49 Commits
Jun 30, 2020
Jun 30, 2020
Jun 25, 2020
Jun 30, 2020
Oct 20, 2017
Jun 17, 2020
Jun 30, 2020
Jun 30, 2020
Oct 23, 2017

Repository files navigation

ExposedObject

Build Status

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:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%