Soenneker.Reflection.Cache 2.1.14

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.14                
NuGet\Install-Package Soenneker.Reflection.Cache -Version 2.1.14                
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.14" />                
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.14                
#r "nuget: Soenneker.Reflection.Cache, 2.1.14"                
#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.14

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

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 (3)

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

Package Downloads
Soenneker.Cosmos.Serializer

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

Soenneker.Utils.AutoBogus

The .NET Bogus autogenerator

Soenneker.Utils.String

A utility library for useful String operations

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.257 3,310 11/20/2024
3.0.256 3,731 11/19/2024
3.0.255 5,457 11/19/2024
3.0.254 57 11/19/2024
3.0.253 57 11/19/2024
3.0.252 60 11/19/2024
3.0.251 3,193 11/19/2024
3.0.250 58 11/19/2024
3.0.249 58 11/19/2024
3.0.248 61 11/19/2024
3.0.247 10,836 11/14/2024
3.0.246 3,686 11/14/2024
3.0.245 4,967 11/14/2024
3.0.244 77 11/14/2024
3.0.243 75 11/14/2024
3.0.242 77 11/14/2024
3.0.241 75 11/14/2024
3.0.240 67 11/14/2024
3.0.239 70 11/14/2024
3.0.238 71 11/14/2024
2.1.237 3,251 11/13/2024
2.1.236 9,130 11/12/2024
2.1.235 279 11/12/2024
2.1.234 71 11/12/2024
2.1.233 9,207 11/8/2024
2.1.232 3,251 11/8/2024
2.1.231 3,444 11/8/2024
2.1.230 13,431 10/31/2024
2.1.229 208 10/31/2024
2.1.228 6,336 10/29/2024
2.1.227 11,285 10/22/2024
2.1.226 7,348 10/22/2024
2.1.225 11,079 10/14/2024
2.1.224 5,439 10/11/2024
2.1.223 796 10/11/2024
2.1.222 7,201 10/8/2024
2.1.221 5,056 10/8/2024
2.1.220 81 10/8/2024
2.1.219 8,718 10/3/2024
2.1.218 8,746 10/2/2024
2.1.217 7,062 10/1/2024
2.1.216 6,914 9/29/2024
2.1.215 1,355 9/28/2024
2.1.214 6,131 9/27/2024
2.1.213 85 9/27/2024
2.1.212 247 9/27/2024
2.1.211 89 9/27/2024
2.1.210 93 9/27/2024
2.1.209 80 9/27/2024
2.1.208 84 9/27/2024
2.1.207 3,092 9/27/2024
2.1.206 8,281 9/26/2024
2.1.205 1,425 9/26/2024
2.1.204 7,163 9/25/2024
2.1.203 5,642 9/23/2024
2.1.202 4,861 9/23/2024
2.1.201 3,743 9/23/2024
2.1.200 87 9/23/2024
2.1.199 789 9/23/2024
2.1.198 82 9/23/2024
2.1.197 533 9/22/2024
2.1.196 91 9/22/2024
2.1.195 10,018 9/17/2024
2.1.194 571 9/17/2024
2.1.193 139 9/17/2024
2.1.192 85 9/17/2024
2.1.191 97 9/17/2024
2.1.190 89 9/17/2024
2.1.189 99 9/17/2024
2.1.188 24,965 9/11/2024
2.1.187 3,619 9/11/2024
2.1.186 9,020 9/10/2024
2.1.185 1,591 9/9/2024
2.1.184 3,122 9/9/2024
2.1.183 6,459 9/9/2024
2.1.182 8,297 9/6/2024
2.1.181 4,617 9/5/2024
2.1.180 2,070 9/5/2024
2.1.179 2,445 9/5/2024
2.1.178 94 9/5/2024
2.1.177 1,028 9/5/2024
2.1.176 98 9/5/2024
2.1.175 99 9/5/2024
2.1.174 8,002 9/4/2024
2.1.173 8,381 9/3/2024
2.1.172 3,015 9/3/2024
2.1.171 14,636 8/21/2024
2.1.170 1,541 8/20/2024
2.1.169 288 8/20/2024
2.1.168 114 8/20/2024
2.1.167 117 8/20/2024
2.1.166 2,534 8/20/2024
2.1.165 114 8/20/2024
2.1.164 106 8/20/2024
2.1.163 7,657 8/19/2024
2.1.162 8,093 8/13/2024
2.1.161 9,479 8/6/2024
2.1.160 6,575 8/1/2024
2.1.159 3,560 7/31/2024
2.1.158 6,548 7/25/2024
2.1.157 59 7/25/2024
2.1.156 2,166 7/24/2024
2.1.155 16,153 7/14/2024
2.1.154 7,622 7/10/2024
2.1.153 3,715 7/10/2024
2.1.151 1,626 7/9/2024
2.1.149 140 7/9/2024
2.1.148 6,107 7/9/2024
2.1.147 3,726 7/9/2024
2.1.146 92 7/9/2024
2.1.145 1,565 7/9/2024
2.1.144 93 7/9/2024
2.1.143 3,413 7/9/2024
2.1.141 599 7/8/2024
2.1.140 85 7/8/2024
2.1.139 87 7/8/2024
2.1.138 1,718 7/8/2024
2.1.137 8,383 7/7/2024
2.1.136 1,030 7/7/2024
2.1.135 12,291 7/3/2024
2.1.134 14,127 6/15/2024
2.1.133 10,555 6/1/2024
2.1.132 76 6/1/2024
2.1.131 610 6/1/2024
2.1.130 5,585 5/31/2024
2.1.129 3,256 5/29/2024
2.1.128 3,691 5/28/2024
2.1.127 2,375 5/27/2024
2.1.126 6,869 5/25/2024
2.1.125 1,852 5/25/2024
2.1.124 148 5/25/2024
2.1.123 104 5/25/2024
2.1.122 1,643 5/25/2024
2.1.121 110 5/25/2024
2.1.120 105 5/25/2024
2.1.119 110 5/25/2024
2.1.118 103 5/25/2024
2.1.117 18,214 5/23/2024
2.1.116 1,896 5/22/2024
2.1.115 1,491 5/22/2024
2.1.114 105 5/22/2024
2.1.113 107 5/22/2024
2.1.112 219 5/22/2024
2.1.111 3,751 5/22/2024
2.1.110 6,576 5/17/2024
2.1.109 6,855 5/14/2024
2.1.108 77 5/14/2024
2.1.107 14,130 4/29/2024
2.1.106 140 4/29/2024
2.1.105 3,769 4/28/2024
2.1.104 2,393 4/28/2024
2.1.103 104 4/28/2024
2.1.102 2,879 4/28/2024
2.1.101 105 4/28/2024
2.1.100 1,666 4/28/2024
2.1.99 108 4/28/2024
2.1.98 103 4/28/2024
2.1.97 105 4/28/2024
2.1.96 631 4/27/2024
2.1.95 105 4/27/2024
2.1.94 1,352 4/27/2024
2.1.93 12,711 4/12/2024
2.1.92 3,199 4/12/2024
2.1.91 112 4/12/2024
2.1.90 123 4/12/2024
2.1.89 152 4/12/2024
2.1.88 168 4/12/2024
2.1.87 115 4/12/2024
2.1.86 2,399 4/12/2024
2.1.85 128 4/12/2024
2.1.84 7,733 4/9/2024
2.1.83 3,779 4/1/2024
2.1.82 7,338 3/25/2024
2.1.81 4,155 3/19/2024
2.1.80 6,158 3/13/2024
2.1.79 117 3/13/2024
2.1.78 999 3/13/2024
2.1.77 115 3/13/2024
2.1.76 112 3/13/2024
2.1.75 2,876 3/12/2024
2.1.74 117 3/12/2024
2.1.73 7,871 3/8/2024
2.1.72 3,771 3/6/2024
2.1.71 2,045 3/4/2024
2.1.70 3,803 3/2/2024
2.1.69 1,603 3/2/2024
2.1.68 2,536 2/29/2024
2.1.67 4,744 2/25/2024
2.1.66 4,095 2/22/2024
2.1.65 2,141 2/21/2024
2.1.64 645 2/21/2024
2.1.63 1,365 2/21/2024
2.1.62 109 2/21/2024
2.1.61 123 2/21/2024
2.1.60 113 2/21/2024
2.1.59 117 2/21/2024
2.1.58 1,174 2/20/2024
2.1.57 3,199 2/20/2024
2.1.56 2,766 2/19/2024
2.1.55 119 2/19/2024
2.1.54 108 2/19/2024
2.1.53 124 2/18/2024
2.1.52 4,081 2/16/2024
2.1.51 119 2/16/2024
2.1.50 791 2/16/2024
2.1.49 1,276 2/16/2024
2.1.48 814 2/16/2024
2.1.47 1,526 2/16/2024
2.1.46 2,175 2/13/2024
2.1.45 2,171 2/13/2024
2.1.44 118 2/13/2024
2.1.43 119 2/13/2024
2.1.42 3,224 2/11/2024
2.1.41 2,305 2/11/2024
2.1.40 1,207 2/10/2024
2.1.39 118 2/10/2024
2.1.38 110 2/10/2024
2.1.37 152 2/9/2024
2.1.36 2,478 2/9/2024
2.1.35 1,547 2/9/2024
2.1.34 118 2/9/2024
2.1.33 1,453 2/8/2024
2.1.32 1,216 2/8/2024
2.1.31 1,164 2/8/2024
2.1.30 2,885 2/6/2024
2.1.29 1,120 2/6/2024
2.1.28 110 2/6/2024
2.1.27 2,265 2/6/2024
2.1.26 120 2/6/2024
2.1.25 169 2/5/2024
2.1.24 2,923 2/4/2024
2.1.23 113 2/4/2024
2.1.22 1,315 2/2/2024
2.1.21 96 2/1/2024
2.1.20 119 2/1/2024
2.1.19 139 2/1/2024
2.1.18 111 1/31/2024
2.1.17 103 1/31/2024
2.1.16 1,735 1/30/2024
2.1.15 4,060 1/28/2024
2.1.14 7,656 1/27/2024
2.1.13 858 1/26/2024
2.1.12 5,977 1/25/2024
2.1.11 108 1/25/2024
2.1.10 107 1/25/2024
2.1.9 111 1/25/2024
2.1.8 105 1/25/2024
2.1.7 2,283 1/23/2024
2.1.6 113 1/21/2024