Soenneker.Reflection.Cache
2.1.6
Prefix Reserved
See the version list below for details.
dotnet add package Soenneker.Reflection.Cache --version 2.1.6
NuGet\Install-Package Soenneker.Reflection.Cache -Version 2.1.6
<PackageReference Include="Soenneker.Reflection.Cache" Version="2.1.6" />
paket add Soenneker.Reflection.Cache --version 2.1.6
#r "nuget: Soenneker.Reflection.Cache, 2.1.6"
// Install Soenneker.Reflection.Cache as a Cake Addin #addin nuget:?package=Soenneker.Reflection.Cache&version=2.1.6 // Install Soenneker.Reflection.Cache as a Cake Tool #tool nuget:?package=Soenneker.Reflection.Cache&version=2.1.6
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()
- 3,482% faster
Method | Mean | Error | StdDev | Ratio | RatioSD |
---|---|---|---|---|---|
GetType_string_NoCache | 950.07 ns | 3.089 ns | 2.738 ns | baseline | |
GetType_string_Cache | 26.60 ns | 0.589 ns | 0.678 ns | 35.82x faster | 1.02x |
GetProperties()
- 5,160% faster
Method | Mean | Error | StdDev | Ratio | RatioSD |
---|---|---|---|---|---|
GetProperties_NoCache | 58.765 ns | 0.6131 ns | 0.5735 ns | baseline | |
GetProperties_Cache | 1.119 ns | 0.0481 ns | 0.0450 ns | 52.59x faster | 2.27x |
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 |
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 |
Product | Versions 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. |
-
.NETStandard 2.1
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 |
---|---|---|
2.1.219 | 2,538 | 10/3/2024 |
2.1.218 | 6,160 | 10/2/2024 |
2.1.217 | 6,107 | 10/1/2024 |
2.1.216 | 6,705 | 9/29/2024 |
2.1.215 | 1,269 | 9/28/2024 |
2.1.214 | 6,078 | 9/27/2024 |
2.1.213 | 69 | 9/27/2024 |
2.1.212 | 227 | 9/27/2024 |
2.1.211 | 64 | 9/27/2024 |
2.1.210 | 77 | 9/27/2024 |
2.1.209 | 65 | 9/27/2024 |
2.1.208 | 69 | 9/27/2024 |
2.1.207 | 3,058 | 9/27/2024 |
2.1.206 | 8,157 | 9/26/2024 |
2.1.205 | 1,401 | 9/26/2024 |
2.1.204 | 7,101 | 9/25/2024 |
2.1.203 | 5,461 | 9/23/2024 |
2.1.202 | 4,831 | 9/23/2024 |
2.1.201 | 3,728 | 9/23/2024 |
2.1.200 | 72 | 9/23/2024 |
2.1.199 | 774 | 9/23/2024 |
2.1.198 | 67 | 9/23/2024 |
2.1.197 | 514 | 9/22/2024 |
2.1.196 | 77 | 9/22/2024 |
2.1.195 | 9,045 | 9/17/2024 |
2.1.194 | 555 | 9/17/2024 |
2.1.193 | 123 | 9/17/2024 |
2.1.192 | 73 | 9/17/2024 |
2.1.191 | 84 | 9/17/2024 |
2.1.190 | 77 | 9/17/2024 |
2.1.189 | 87 | 9/17/2024 |
2.1.188 | 24,058 | 9/11/2024 |
2.1.187 | 3,608 | 9/11/2024 |
2.1.186 | 8,950 | 9/10/2024 |
2.1.185 | 1,580 | 9/9/2024 |
2.1.184 | 3,111 | 9/9/2024 |
2.1.183 | 6,449 | 9/9/2024 |
2.1.182 | 8,283 | 9/6/2024 |
2.1.181 | 4,604 | 9/5/2024 |
2.1.180 | 2,062 | 9/5/2024 |
2.1.179 | 2,419 | 9/5/2024 |
2.1.178 | 85 | 9/5/2024 |
2.1.177 | 1,020 | 9/5/2024 |
2.1.176 | 88 | 9/5/2024 |
2.1.175 | 91 | 9/5/2024 |
2.1.174 | 7,979 | 9/4/2024 |
2.1.173 | 8,371 | 9/3/2024 |
2.1.172 | 3,008 | 9/3/2024 |
2.1.171 | 14,301 | 8/21/2024 |
2.1.170 | 1,532 | 8/20/2024 |
2.1.169 | 284 | 8/20/2024 |
2.1.168 | 110 | 8/20/2024 |
2.1.167 | 113 | 8/20/2024 |
2.1.166 | 2,530 | 8/20/2024 |
2.1.165 | 108 | 8/20/2024 |
2.1.164 | 102 | 8/20/2024 |
2.1.163 | 7,653 | 8/19/2024 |
2.1.162 | 8,024 | 8/13/2024 |
2.1.161 | 9,462 | 8/6/2024 |
2.1.160 | 5,517 | 8/1/2024 |
2.1.159 | 3,550 | 7/31/2024 |
2.1.158 | 5,703 | 7/25/2024 |
2.1.157 | 54 | 7/25/2024 |
2.1.156 | 2,161 | 7/24/2024 |
2.1.155 | 15,418 | 7/14/2024 |
2.1.154 | 7,612 | 7/10/2024 |
2.1.153 | 3,711 | 7/10/2024 |
2.1.151 | 1,622 | 7/9/2024 |
2.1.149 | 136 | 7/9/2024 |
2.1.148 | 6,103 | 7/9/2024 |
2.1.147 | 3,723 | 7/9/2024 |
2.1.146 | 89 | 7/9/2024 |
2.1.145 | 1,562 | 7/9/2024 |
2.1.144 | 90 | 7/9/2024 |
2.1.143 | 3,410 | 7/9/2024 |
2.1.141 | 596 | 7/8/2024 |
2.1.140 | 82 | 7/8/2024 |
2.1.139 | 84 | 7/8/2024 |
2.1.138 | 1,715 | 7/8/2024 |
2.1.137 | 8,372 | 7/7/2024 |
2.1.136 | 1,027 | 7/7/2024 |
2.1.135 | 11,779 | 7/3/2024 |
2.1.134 | 13,546 | 6/15/2024 |
2.1.133 | 10,254 | 6/1/2024 |
2.1.132 | 73 | 6/1/2024 |
2.1.131 | 607 | 6/1/2024 |
2.1.130 | 5,582 | 5/31/2024 |
2.1.129 | 3,227 | 5/29/2024 |
2.1.128 | 3,688 | 5/28/2024 |
2.1.127 | 2,372 | 5/27/2024 |
2.1.126 | 6,861 | 5/25/2024 |
2.1.125 | 1,849 | 5/25/2024 |
2.1.124 | 145 | 5/25/2024 |
2.1.123 | 101 | 5/25/2024 |
2.1.122 | 1,640 | 5/25/2024 |
2.1.121 | 107 | 5/25/2024 |
2.1.120 | 102 | 5/25/2024 |
2.1.119 | 107 | 5/25/2024 |
2.1.118 | 100 | 5/25/2024 |
2.1.117 | 15,670 | 5/23/2024 |
2.1.116 | 1,893 | 5/22/2024 |
2.1.115 | 1,488 | 5/22/2024 |
2.1.114 | 101 | 5/22/2024 |
2.1.113 | 104 | 5/22/2024 |
2.1.112 | 216 | 5/22/2024 |
2.1.111 | 3,738 | 5/22/2024 |
2.1.110 | 6,543 | 5/17/2024 |
2.1.109 | 6,852 | 5/14/2024 |
2.1.108 | 74 | 5/14/2024 |
2.1.107 | 12,476 | 4/29/2024 |
2.1.106 | 137 | 4/29/2024 |
2.1.105 | 3,759 | 4/28/2024 |
2.1.104 | 2,389 | 4/28/2024 |
2.1.103 | 100 | 4/28/2024 |
2.1.102 | 2,875 | 4/28/2024 |
2.1.101 | 101 | 4/28/2024 |
2.1.100 | 1,662 | 4/28/2024 |
2.1.99 | 100 | 4/28/2024 |
2.1.98 | 97 | 4/28/2024 |
2.1.97 | 101 | 4/28/2024 |
2.1.96 | 625 | 4/27/2024 |
2.1.95 | 102 | 4/27/2024 |
2.1.94 | 1,349 | 4/27/2024 |
2.1.93 | 12,704 | 4/12/2024 |
2.1.92 | 3,196 | 4/12/2024 |
2.1.91 | 107 | 4/12/2024 |
2.1.90 | 120 | 4/12/2024 |
2.1.89 | 149 | 4/12/2024 |
2.1.88 | 165 | 4/12/2024 |
2.1.87 | 112 | 4/12/2024 |
2.1.86 | 2,396 | 4/12/2024 |
2.1.85 | 125 | 4/12/2024 |
2.1.84 | 7,730 | 4/9/2024 |
2.1.83 | 3,760 | 4/1/2024 |
2.1.82 | 7,058 | 3/25/2024 |
2.1.81 | 4,145 | 3/19/2024 |
2.1.80 | 6,155 | 3/13/2024 |
2.1.79 | 114 | 3/13/2024 |
2.1.78 | 996 | 3/13/2024 |
2.1.77 | 112 | 3/13/2024 |
2.1.76 | 109 | 3/13/2024 |
2.1.75 | 2,873 | 3/12/2024 |
2.1.74 | 114 | 3/12/2024 |
2.1.73 | 7,866 | 3/8/2024 |
2.1.72 | 3,768 | 3/6/2024 |
2.1.71 | 2,040 | 3/4/2024 |
2.1.70 | 3,800 | 3/2/2024 |
2.1.69 | 1,600 | 3/2/2024 |
2.1.68 | 2,533 | 2/29/2024 |
2.1.67 | 4,741 | 2/25/2024 |
2.1.66 | 3,953 | 2/22/2024 |
2.1.65 | 2,138 | 2/21/2024 |
2.1.64 | 642 | 2/21/2024 |
2.1.63 | 1,362 | 2/21/2024 |
2.1.62 | 106 | 2/21/2024 |
2.1.61 | 120 | 2/21/2024 |
2.1.60 | 110 | 2/21/2024 |
2.1.59 | 114 | 2/21/2024 |
2.1.58 | 1,171 | 2/20/2024 |
2.1.57 | 3,194 | 2/20/2024 |
2.1.56 | 2,759 | 2/19/2024 |
2.1.55 | 116 | 2/19/2024 |
2.1.54 | 105 | 2/19/2024 |
2.1.53 | 121 | 2/18/2024 |
2.1.52 | 4,078 | 2/16/2024 |
2.1.51 | 114 | 2/16/2024 |
2.1.50 | 786 | 2/16/2024 |
2.1.49 | 1,270 | 2/16/2024 |
2.1.48 | 809 | 2/16/2024 |
2.1.47 | 1,521 | 2/16/2024 |
2.1.46 | 2,170 | 2/13/2024 |
2.1.45 | 2,168 | 2/13/2024 |
2.1.44 | 115 | 2/13/2024 |
2.1.43 | 114 | 2/13/2024 |
2.1.42 | 3,221 | 2/11/2024 |
2.1.41 | 2,300 | 2/11/2024 |
2.1.40 | 1,204 | 2/10/2024 |
2.1.39 | 115 | 2/10/2024 |
2.1.38 | 107 | 2/10/2024 |
2.1.37 | 147 | 2/9/2024 |
2.1.36 | 2,475 | 2/9/2024 |
2.1.35 | 1,544 | 2/9/2024 |
2.1.34 | 113 | 2/9/2024 |
2.1.33 | 1,450 | 2/8/2024 |
2.1.32 | 1,213 | 2/8/2024 |
2.1.31 | 1,159 | 2/8/2024 |
2.1.30 | 2,878 | 2/6/2024 |
2.1.29 | 1,113 | 2/6/2024 |
2.1.28 | 102 | 2/6/2024 |
2.1.27 | 2,258 | 2/6/2024 |
2.1.26 | 111 | 2/6/2024 |
2.1.25 | 166 | 2/5/2024 |
2.1.24 | 2,920 | 2/4/2024 |
2.1.23 | 108 | 2/4/2024 |
2.1.22 | 1,310 | 2/2/2024 |
2.1.21 | 93 | 2/1/2024 |
2.1.20 | 114 | 2/1/2024 |
2.1.19 | 136 | 2/1/2024 |
2.1.18 | 108 | 1/31/2024 |
2.1.17 | 98 | 1/31/2024 |
2.1.16 | 1,731 | 1/30/2024 |
2.1.15 | 4,055 | 1/28/2024 |
2.1.14 | 7,651 | 1/27/2024 |
2.1.13 | 855 | 1/26/2024 |
2.1.12 | 5,972 | 1/25/2024 |
2.1.11 | 105 | 1/25/2024 |
2.1.10 | 102 | 1/25/2024 |
2.1.9 | 106 | 1/25/2024 |
2.1.8 | 102 | 1/25/2024 |
2.1.7 | 2,278 | 1/23/2024 |
2.1.6 | 110 | 1/21/2024 |