ReflectionMagic 5.0.1
dotnet add package ReflectionMagic --version 5.0.1
NuGet\Install-Package ReflectionMagic -Version 5.0.1
<PackageReference Include="ReflectionMagic" Version="5.0.1" />
paket add ReflectionMagic --version 5.0.1
#r "nuget: ReflectionMagic, 5.0.1"
// 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
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 | Versions 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. |
-
.NETFramework 4.6.2
- Microsoft.CSharp (>= 4.7.0)
-
.NETStandard 2.0
- Microsoft.CSharp (>= 4.7.0)
-
net6.0
- No dependencies.
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 | 144,469 | 1/29/2024 |
5.0.0 | 152,171 | 6/9/2023 |
4.1.0 | 2,588,989 | 6/24/2019 |
4.1.0-beta1 | 1,079 | 6/17/2019 |
4.0.0 | 136,094 | 11/15/2018 |
4.0.0-beta1 | 1,256 | 9/10/2018 |
3.2.0-beta1 | 4,352 | 5/29/2018 |
3.1.0 | 546,129 | 11/13/2017 |
3.1.0-beta1 | 1,365 | 11/1/2017 |
3.0.0 | 121,947 | 3/9/2017 |
3.0.0-beta5 | 2,582 | 12/27/2016 |
3.0.0-beta1 | 1,530 | 11/22/2016 |
2.1.0 | 110,965 | 12/31/2014 |
2.0.3 | 54,797 | 10/14/2014 |
2.0.2 | 22,166 | 3/7/2014 |
2.0.0 | 199,112 | 4/26/2013 |
1.1.0 | 102,984 | 10/27/2011 |
1.0.1 | 48,925 | 8/10/2011 |