ReflectionMagic 5.0.1

dotnet add package ReflectionMagic --version 5.0.1                
NuGet\Install-Package ReflectionMagic -Version 5.0.1                
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="ReflectionMagic" Version="5.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ReflectionMagic --version 5.0.1                
#r "nuget: ReflectionMagic, 5.0.1"                
#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 ReflectionMagic as a Cake Addin
#addin nuget:?package=ReflectionMagic&version=5.0.1

// Install ReflectionMagic as a Cake Tool
#tool nuget:?package=ReflectionMagic&version=5.0.1                

ReflectionMagic

Build Status

Private reflection allows you to access private and internal members in other assemblies. Generally, it’s considered to be a bad thing to do, as it ties you to undocumented implementation details which can later break you. Also, it’s not usable in medium trust.

The purpose of this library is not to encourage anyone to use private reflection in situations where you would not have done it anyway. Instead, the purpose is to allow you to do it much more easily if you decide that you need to use it.

Putting it a different way, I’m not telling you to break the law, but I’m telling you how to break the law more efficiently if that’s what you’re into!

The scenario

Assume you are using an assembly that has code like this:

public class Foo1 
{
    private Foo2 GetOtherClass() 
    { 
        // Omitted
    }
}

internal class Foo2 
{
    private string SomeProp { get { /* Omitted */ } }
}

And assume you have an instance foo1 of the public class Foo1 and your evil self tells you that you want to call the private method GetOtherClass() and then get the SomeProp property off that.

Using reflection

Using plain old reflection this would be something like this:

object foo2 = typeof(Foo1).InvokeMember("GetOtherClass", 
                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
                null, foo1, null);
                
PropertyInfo propInfo = foo2.GetType().GetProperty("SomeProp",    
                BindingFlags.Instance | BindingFlags.NonPublic);

string val = (string)propInfo.GetValue(foo2, null);

Which works, but is pretty ugly.

Using ReflectionMagic

Doing the same but using the ReflectionMagic library:

string val = foo1.AsDynamic().GetOtherClass().SomeProp;

Download

This library is available through NuGet.

More info

For more information look at the original blog post by David Ebbo: https://blogs.msdn.microsoft.com/davidebb/2010/01/18/use-c-4-0-dynamic-to-drastically-simplify-your-private-reflection-code/

Known limitations

Support for 'out' and 'ref' parameters is not available on .NET Core 1.x runtimes. This is a runtime limitation and results in a PlatformNotSupportedException.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 net461 was computed.  net462 is compatible.  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.

NuGet packages (27)

Showing the top 5 NuGet packages that depend on ReflectionMagic:

Package Downloads
Lime.Transport.WebSocket

C# LIME web socket transport library

RazorGenerator.Testing

Helpers to allow unit testing precompiled Razor views

Wyam.Less

Wyam is a simple to use, highly modular, and extremely configurable static content generator. This library provides support for parsing Less CSS content.

Kledex

Kledex is a .NET Core framework that can be used to create a simple and clean design by enforcing single responsibility and separation of concerns. Its advanced features are ideal for Domain Driven Design (DDD), Command Query Responsibilty Segragation (CQRS) and Event Sourcing. Kledex also has Azure Service Bus and RabbitMQ integrations.

MoeLib.Web

MoeLib.Web

GitHub repositories (9)

Showing the top 5 popular GitHub repositories that depend on ReflectionMagic:

Repository Stars
testcontainers/testcontainers-dotnet
A library to support tests with throwaway instances of Docker containers for all compatible .NET Standard versions.
rnwood/smtp4dev
smtp4dev - the fake smtp email server for development and testing
UiPath/CoreWF
WF runtime ported to work on .NET 6
CodeMazeBlog/CodeMazeGuides
The main repository for all the Code Maze guides
RazorGenerator/RazorGenerator
A Custom Tool for Visual Studio that allows processing Razor files at design time instead of runtime, allowing them to be built into an assembly for simpler reuse and distribution.
Version Downloads Last updated
5.0.1 127,488 1/29/2024
5.0.0 145,451 6/9/2023
4.1.0 2,537,058 6/24/2019
4.1.0-beta1 1,075 6/17/2019
4.0.0 135,553 11/15/2018
4.0.0-beta1 1,247 9/10/2018
3.2.0-beta1 4,343 5/29/2018
3.1.0 545,097 11/13/2017
3.1.0-beta1 1,356 11/1/2017
3.0.0 121,575 3/9/2017
3.0.0-beta5 2,573 12/27/2016
3.0.0-beta1 1,521 11/22/2016
2.1.0 110,037 12/31/2014
2.0.3 54,568 10/14/2014
2.0.2 21,384 3/7/2014
2.0.0 197,793 4/26/2013
1.1.0 102,771 10/27/2011
1.0.1 48,171 8/10/2011