Soenneker.Reflection.Cache 2.1.7

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Soenneker.Reflection.Cache --version 2.1.7
NuGet\Install-Package Soenneker.Reflection.Cache -Version 2.1.7
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="Soenneker.Reflection.Cache" Version="2.1.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Soenneker.Reflection.Cache --version 2.1.7
#r "nuget: Soenneker.Reflection.Cache, 2.1.7"
#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 Soenneker.Reflection.Cache as a Cake Addin
#addin nuget:?package=Soenneker.Reflection.Cache&version=2.1.7

// Install Soenneker.Reflection.Cache as a Cake Tool
#tool nuget:?package=Soenneker.Reflection.Cache&version=2.1.7

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

alternate text is missing from this package README image Soenneker.Reflection.Cache

The fastest .NET Reflection cache

Reflection is slow.

  • If you're calling some Reflection code once, consider if creating a cache is necessary.
  • If you need to call Reflection repeatedly, this library can help speed things up.

This library is attempting to be a drop-in replacement for System.Reflection and caches the results of Reflection calls (so it's going to allocate more memory). It's thread-safe and supports concurrency.

Installation

dotnet add package Soenneker.Reflection.Cache

This cache can either be added to DI like so:

public void ConfigureServices(IServiceCollection services)
{
    services.AddReflectionCacheAsSingleton(); // or AddReflectionCacheAsScoped()
}

and you could access it like:

public class MyService
{
    private readonly IReflectionCache _cache;

    public MyService(IReflectionCache cache)
    {
        _cache = cache;
    }
}

or you can instantiate it manually:

var cache = new ReflectionCache();

Usage

var type1 = Type.GetType("System.String"); // <-- regular Reflection
var type2 = cache.GetType("System.String"); // <-- cached Reflection
bool areEqual = type1 == type2; // true

Keep in mind:

cache.GetType("System.String"); // <-- as slow as regular Reflection
cache.GetType("System.String"); // <-- very fast because the first call was cached

⚠️ Important ⚠️

Be mindful of the "cache chain". Use the Cached methods and types until you need to get the final Reflection type you need from the cache.

There are two methods for most operations like this:

Type typeofString = cache.GetType("System.String"); // <-- caches, stops the cache chain
CachedType type = cache.GetCachedType("System.String"); // <-- caches, continues the cache chain
Scenario: Retrieving parameters from a method

✅ Good:

CachedType cachedType = cache.GetCachedType("System.String");
CachedMethod cachedMethodInfo = cachedType.GetCachedMethod("Intern");
ParameterInfo?[] parameters = cachedMethodInfo.GetParameters(); // < -- parameters are now cached

❌ Bad:

CachedType cachedType = cache.GetCachedType("System.String");
MethodInfo methodInfo = cachedType.GetMethod("Intern"); // <-- uh oh, a non-cached Reflection type
ParameterInfo?[] parameters = methodInfo.GetParameters(); // <-- not cached, repeat calls are slow

Notes

  • Be thoughtful of your memory footprint and where/when you dispose of the cache.
  • A cache removal mechanism is needing to be built yet.
  • Many Reflection functionalities are not yet implemented, and could benefit from caching.
  • If you see something that could be improved (performance or allocation), please open an issue or PR.
  • This library is not yet battle-tested. Please use with caution.

Benchmarks (.NET 8.0)

GetType() 5,772% faster

Method Mean Error StdDev Ratio RatioSD
GetType_string_NoCache 955.27 ns 2.216 ns 2.073 ns baseline
GetType_string_Cache 16.27 ns 0.102 ns 0.091 ns 58.72x faster 0.38x
GetType_string_ThreadSafe_Cache 23.99 ns 0.402 ns 0.376 ns 39.83x faster 0.64x

GetProperties() 8,960% faster

Method Mean Error StdDev Ratio RatioSD
GetProperties_NoCache 58.5363 ns 0.3463 ns 0.3070 ns baseline
GetProperties_Cache 0.6502 ns 0.0370 ns 0.0328 ns 90.25x faster 4.59x
GetProperties_ThreadSafe_Cache 0.7169 ns 0.0129 ns 0.0108 ns 81.72x faster 1.36x

GetMethods() 599% faster

Method Mean Error StdDev Ratio RatioSD
GetMethods_NoCache 275.22 ns 1.899 ns 1.776 ns baseline
GetMethods_Cache 39.36 ns 0.694 ns 0.649 ns 6.99x faster 0.13x

GetCustomAttributes() 1,319% faster

Method Mean Error StdDev Ratio RatioSD
GetAttributes_NoCache 1,982.84 ns 14.271 ns 12.651 ns baseline
GetAttributes_Cache 14.87 ns 0.358 ns 0.351 ns 132.928x faster 3.33x

GetMethod() 37% faster

Method Mean Error StdDev Ratio RatioSD
GetMethod_NoCache 23.06 ns 0.234 ns 0.208 ns baseline
GetMethod_Cache 16.77 ns 0.079 ns 0.070 ns 1.37x faster 0.01x

GetMembers() 71,130% faster

Method Mean Error StdDev Ratio RatioSD
GetMembers_NoCache 519.4286 ns 1.7392 ns 1.5417 ns baseline
GetMembers_Cache 0.7297 ns 0.0089 ns 0.0083 ns 712.321x faster 7.66x

GetProperty() 52% faster

Method Mean Error StdDev Ratio RatioSD
GetProperty_NoCache 25.71 ns 0.295 ns 0.276 ns baseline
GetProperty_Cache 16.89 ns 0.171 ns 0.151 ns 1.52x faster 0.03x

GetGenericTypeDefinition() 420% faster

Method Mean Error StdDev Ratio RatioSD
GetGenericTypeDefinition_NoCache 1.8214 ns 0.0651 ns 0.0577 ns baseline
GetGenericTypeDefinition_Cache 0.3505 ns 0.0123 ns 0.0109 ns 5.20x faster 0.26x

IsAssignableFrom() 36% faster

Method Mean Error StdDev Ratio RatioSD
IsAssignableFrom_NoCache 11.054 ns 0.2127 ns 0.1989 ns baseline
IsAssignableFrom_Cache 8.133 ns 0.1196 ns 0.1119 ns 1.36x faster 0.03x

Notes:

  • These are averages over many iterations. The first operation is going to be as slow as the Reflection it sits in front of.
  • Outliers have been removed in cases BenchmarkDotnet deems necessary.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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 (2)

Showing the top 2 NuGet packages that depend on Soenneker.Reflection.Cache:

Package Downloads
Soenneker.Cosmos.Serializer The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

A fast, lightweight JSON (de)serializer for Azure Cosmos DB

Soenneker.Utils.AutoBogus The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

The .NET Bogus autogenerator

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.110 342 5/17/2024
2.1.109 4,937 5/14/2024
2.1.108 46 5/14/2024
2.1.107 7,904 4/29/2024
2.1.106 101 4/29/2024
2.1.105 3,482 4/28/2024
2.1.104 2,234 4/28/2024
2.1.103 77 4/28/2024
2.1.102 2,806 4/28/2024
2.1.101 80 4/28/2024
2.1.100 1,579 4/28/2024
2.1.99 76 4/28/2024
2.1.98 74 4/28/2024
2.1.97 76 4/28/2024
2.1.96 451 4/27/2024
2.1.95 76 4/27/2024
2.1.94 992 4/27/2024
2.1.93 11,895 4/12/2024
2.1.92 2,775 4/12/2024
2.1.91 85 4/12/2024
2.1.90 99 4/12/2024
2.1.89 97 4/12/2024
2.1.88 101 4/12/2024
2.1.87 91 4/12/2024
2.1.86 2,215 4/12/2024
2.1.85 97 4/12/2024
2.1.84 6,630 4/9/2024
2.1.83 3,495 4/1/2024
2.1.82 5,575 3/25/2024
2.1.81 4,031 3/19/2024
2.1.80 6,049 3/13/2024
2.1.79 81 3/13/2024
2.1.78 912 3/13/2024
2.1.77 82 3/13/2024
2.1.76 83 3/13/2024
2.1.75 2,723 3/12/2024
2.1.74 87 3/12/2024
2.1.73 7,674 3/8/2024
2.1.72 3,662 3/6/2024
2.1.71 1,961 3/4/2024
2.1.70 3,699 3/2/2024
2.1.69 1,511 3/2/2024
2.1.68 2,428 2/29/2024
2.1.67 4,585 2/25/2024
2.1.66 3,629 2/22/2024
2.1.65 2,056 2/21/2024
2.1.64 573 2/21/2024
2.1.63 1,293 2/21/2024
2.1.62 84 2/21/2024
2.1.61 97 2/21/2024
2.1.60 87 2/21/2024
2.1.59 90 2/21/2024
2.1.58 1,093 2/20/2024
2.1.57 3,073 2/20/2024
2.1.56 2,658 2/19/2024
2.1.55 94 2/19/2024
2.1.54 95 2/19/2024
2.1.53 98 2/18/2024
2.1.52 3,974 2/16/2024
2.1.51 88 2/16/2024
2.1.50 733 2/16/2024
2.1.49 1,200 2/16/2024
2.1.48 745 2/16/2024
2.1.47 1,443 2/16/2024
2.1.46 2,097 2/13/2024
2.1.45 2,071 2/13/2024
2.1.44 88 2/13/2024
2.1.43 91 2/13/2024
2.1.42 3,107 2/11/2024
2.1.41 2,211 2/11/2024
2.1.40 1,151 2/10/2024
2.1.39 89 2/10/2024
2.1.38 86 2/10/2024
2.1.37 108 2/9/2024
2.1.36 2,394 2/9/2024
2.1.35 1,492 2/9/2024
2.1.34 88 2/9/2024
2.1.33 1,373 2/8/2024
2.1.32 1,155 2/8/2024
2.1.31 1,116 2/8/2024
2.1.30 2,803 2/6/2024
2.1.29 1,063 2/6/2024
2.1.28 78 2/6/2024
2.1.27 2,165 2/6/2024
2.1.26 86 2/6/2024
2.1.25 119 2/5/2024
2.1.24 2,848 2/4/2024
2.1.23 75 2/4/2024
2.1.22 1,273 2/2/2024
2.1.21 80 2/1/2024
2.1.20 81 2/1/2024
2.1.19 97 2/1/2024
2.1.18 84 1/31/2024
2.1.17 77 1/31/2024
2.1.16 1,678 1/30/2024
2.1.15 3,944 1/28/2024
2.1.14 7,411 1/27/2024
2.1.13 768 1/26/2024
2.1.12 5,799 1/25/2024
2.1.11 83 1/25/2024
2.1.10 79 1/25/2024
2.1.9 82 1/25/2024
2.1.8 78 1/25/2024
2.1.7 2,174 1/23/2024
2.1.6 88 1/21/2024