{"id":24271894,"url":"https://github.com/soenneker/soenneker.reflection.cache","last_synced_at":"2026-03-15T03:26:20.646Z","repository":{"id":218401793,"uuid":"744287514","full_name":"soenneker/soenneker.reflection.cache","owner":"soenneker","description":"The fastest .NET Reflection cache","archived":false,"fork":false,"pushed_at":"2026-02-21T01:44:16.000Z","size":1039,"stargazers_count":15,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2026-02-21T06:52:13.932Z","etag":null,"topics":["cache","csharp","dotnet","fast","reflection","reflectioncache","util"],"latest_commit_sha":null,"homepage":"https://soenneker.com","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soenneker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"soenneker","buy_me_a_coffee":"soenneker","thanks_dev":"soenneker","ko_fi":"soenneker","patreon":"soenneker"}},"created_at":"2024-01-17T01:38:09.000Z","updated_at":"2026-02-21T00:35:13.000Z","dependencies_parsed_at":"2024-03-19T05:31:08.509Z","dependency_job_id":"84dd2f6a-f630-482f-a54e-4e63abfc63d5","html_url":"https://github.com/soenneker/soenneker.reflection.cache","commit_stats":null,"previous_names":["soenneker/soenneker.reflection.cache"],"tags_count":63,"template":false,"template_full_name":null,"purl":"pkg:github/soenneker/soenneker.reflection.cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.reflection.cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.reflection.cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.reflection.cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.reflection.cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soenneker","download_url":"https://codeload.github.com/soenneker/soenneker.reflection.cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.reflection.cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29765745,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-23T21:02:23.375Z","status":"ssl_error","status_checked_at":"2026-02-23T20:58:31.539Z","response_time":90,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","csharp","dotnet","fast","reflection","reflectioncache","util"],"created_at":"2025-01-15T17:59:56.366Z","updated_at":"2026-03-10T00:06:40.961Z","avatar_url":"https://github.com/soenneker.png","language":"C#","funding_links":["https://github.com/sponsors/soenneker","https://buymeacoffee.com/soenneker","https://thanks.dev/soenneker","https://ko-fi.com/soenneker","https://patreon.com/soenneker"],"categories":[],"sub_categories":[],"readme":"[![](https://img.shields.io/nuget/v/soenneker.reflection.cache.svg?style=for-the-badge)](https://www.nuget.org/packages/soenneker.reflection.cache/)\n[![](https://img.shields.io/github/actions/workflow/status/soenneker/soenneker.reflection.cache/publish-package.yml?style=for-the-badge)](https://github.com/soenneker/soenneker.reflection.cache/actions/workflows/publish-package.yml)\n[![](https://img.shields.io/nuget/dt/soenneker.reflection.cache.svg?style=for-the-badge)](https://www.nuget.org/packages/soenneker.reflection.cache/)\n[![](https://img.shields.io/github/actions/workflow/status/soenneker/soenneker.reflection.cache/codeql.yml?label=CodeQL\u0026style=for-the-badge)](https://github.com/soenneker/soenneker.reflection.cache/actions/workflows/codeql.yml)\n\n# ![](https://user-images.githubusercontent.com/4441470/224455560-91ed3ee7-f510-4041-a8d2-3fc093025112.png) Soenneker.Reflection.Cache\n### The fastest .NET Reflection cache\n\n## Why?\n\nSystem.Reflection is [slow](https://learn.microsoft.com/en-us/archive/msdn-magazine/2005/july/using-net-avoid-common-performance-pitfalls-for-speedier-apps). If you need to call Reflection code repeatedly, this library can *drastically* speed up subsequent calls. It's thread-safe and supports concurrency.\n\n\n## Installation\n\n```\ndotnet add package Soenneker.Reflection.Cache\n```\n\nThis cache can either be added to DI like so:\n\n```csharp\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddReflectionCacheAsSingleton(); // or AddReflectionCacheAsScoped()\n}\n```\n\nand you could access it like:\n\n```csharp\npublic class MyService\n{\n    private readonly IReflectionCache _cache;\n\n    public MyService(IReflectionCache cache)\n    {\n        _cache = cache;\n    }\n}\n```\n\nor you can instantiate it manually:\n\n```csharp\nvar cache = new ReflectionCache(threadSafe: true); // can be disabled for extra speed\n```\n\n## Usage\n\n```csharp\nvar type1 = Type.GetType(\"System.String\"); // \u003c-- regular Reflection\nvar type2 = cache.GetType(\"System.String\"); // \u003c-- cached Reflection\nbool areEqual = type1 == type2; // true\n```\n\nKeep in mind:\n\n```csharp\ncache.GetType(\"System.String\"); // \u003c-- as slow as regular Reflection\ncache.GetType(\"System.String\"); // \u003c-- very fast because the first call was cached\n```\n\n### ⚠️ Important ⚠️\n\nBe 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.\n\nThere are two methods for most operations like this:\n\n```csharp\nType typeofString = cache.GetType(\"System.String\"); // \u003c-- caches, stops the cache chain\nCachedType type = cache.GetCachedType(\"System.String\"); // \u003c-- caches, continues the cache chain\n```\n\n\n#### Scenario: Retrieving parameters from a method\n\n✅ Good:\n\n```csharp\nCachedType cachedType = cache.GetCachedType(\"System.String\");\nCachedMethod cachedMethodInfo = cachedType.GetCachedMethod(\"Intern\");\nParameterInfo?[] parameters = cachedMethodInfo.GetParameters(); // \u003c -- parameters are now cached\n```\n\n❌ Bad: \n\n```csharp\nCachedType cachedType = cache.GetCachedType(\"System.String\");\nMethodInfo methodInfo = cachedType.GetMethod(\"Intern\"); // \u003c-- uh oh, a non-cached Reflection type\nParameterInfo?[] parameters = methodInfo.GetParameters(); // \u003c-- not cached, repeat calls are slow\n```\n\n### Tips\n\n- Almost all of the `Cached` methods (e.g. `GetCachedParameters()` vs `GetParameters()` are faster due to the final preparation needed to match the System.Reflection methods. \n- Work with the `Cached` objects instead of Reflection objects if possible. They're faster to retrieve and allow for more efficient downstream chaining.\n- Thread safety can be disabled for more speed. It's enabled by default.\n- Consider if caching is even necessary for your use case. If you're only calling Reflection once, it may not be worth it.\n- Caching isn't free. Be thoughtful of your memory footprint and where/when you dispose of the cache.\n\n### Notes\n\n- A cache removal mechanism is needing to be built yet.\n- Many Reflection functionalities are not yet implemented, and could benefit from caching.\n- If you see something that could be improved (performance or allocation), please open an issue or PR.\n\n---\n\n## Benchmarks (.NET)\n\n### `GetType()` 5,772% faster\n\n| Method                              | Mean        | Error    | StdDev   | Ratio         | RatioSD |\n|------------------------------------ |------------:|---------:|---------:|--------------:|--------:|\n| GetType_string_NoCache              | 1,022.30 ns | 9.462 ns | 8.851 ns |      baseline |         |\n| GetType_string_Cache                |    17.52 ns | 0.303 ns | 0.283 ns | 58.38x faster |   1.12x |\n| GetType_string_threadSafe_Cache     |    24.73 ns | 0.139 ns | 0.116 ns | 41.29x faster |   0.33x |\n| GetCachedType_type_Cache            |    12.21 ns | 0.234 ns | 0.218 ns | 83.76x faster |   1.74x |\n| GetCachedType_type_ThreadSafe_Cache |    19.01 ns | 0.067 ns | 0.052 ns | 53.73x faster |   0.47x |\n\n### `GetMethods()` 24,842% faster\n\n| Method             | Mean       | Error     | StdDev    | Ratio           | RatioSD |\n|------------------- |-----------:|----------:|----------:|----------------:|--------:|\n| GetMethods_NoCache | 256.526 ns | 1.4587 ns | 1.2180 ns |        baseline |         |\n| GetMethods_Cache   |   1.030 ns | 0.0412 ns | 0.0385 ns | 249.428x faster |  10.30x |\n\n### `GetMethod()` 37% faster\n\n| Method            | Mean     | Error    | StdDev   | Ratio        | RatioSD |\n|------------------ |---------:|---------:|---------:|-------------:|--------:|\n| GetMethod_NoCache | 23.06 ns | 0.234 ns | 0.208 ns |     baseline |         |\n| GetMethod_Cache   | 16.77 ns | 0.079 ns | 0.070 ns | 1.37x faster |   0.01x |\n\n### `GetMembers()` 83,924% faster\n\n| Method                      | Mean        | Error     | StdDev    | Ratio           | RatioSD |\n|---------------------------- |------------:|----------:|----------:|----------------:|--------:|\n| GetMembers_NoCache          | 550.2334 ns | 4.1411 ns | 3.8736 ns |        baseline |         |\n| GetMembers_Cache            |   0.6579 ns | 0.0515 ns | 0.0481 ns | 840.247x faster |  58.17x |\n| GetMembers_ThreadSafe_Cache |   0.7273 ns | 0.0307 ns | 0.0287 ns | 757.728x faster |  31.76x |\n\n### `GetMember()` 1,043% faster\n\n| Method            | Mean      | Error    | StdDev   | Ratio         | RatioSD |\n|------------------ |----------:|---------:|---------:|--------------:|--------:|\n| GetMember_NoCache | 136.57 ns | 1.353 ns | 1.266 ns |      baseline |         |\n| GetMember_Cache   |  11.95 ns | 0.091 ns | 0.081 ns | 11.43x faster |   0.12x |\n\n### `GetProperties()` 8,960% faster\n\n| Method                         | Mean       | Error     | StdDev    | Ratio         | RatioSD |\n|------------------------------- |-----------:|----------:|----------:|--------------:|--------:|\n| GetProperties_NoCache          | 58.5363 ns | 0.3463 ns | 0.3070 ns |      baseline |         |\n| GetProperties_Cache            |  0.6502 ns | 0.0370 ns | 0.0328 ns | 90.25x faster |   4.59x |\n| GetProperties_ThreadSafe_Cache |  0.7169 ns | 0.0129 ns | 0.0108 ns | 81.72x faster |   1.36x |\n\n### `GetProperty()` 57% faster\n\n| Method              | Mean     | Error    | StdDev   | Ratio        | RatioSD |\n|-------------------- |---------:|---------:|---------:|-------------:|--------:|\n| GetProperty_NoCache | 25.61 ns | 0.382 ns | 0.357 ns |     baseline |         |\n| GetProperty_Cache   | 16.23 ns | 0.074 ns | 0.062 ns | 1.57x faster |   0.01x |\n\n### `GetFields()` 419% faster\n\n| Method                     | Mean      | Error     | StdDev    | Ratio         | RatioSD |\n|--------------------------- |----------:|----------:|----------:|--------------:|--------:|\n| GetFields_NoCache          | 48.941 ns | 0.9092 ns | 0.8505 ns |      baseline |         |\n| GetFields_Cache            |  1.141 ns | 0.0512 ns | 0.0479 ns | 42.95x faster |   1.32x |\n| GetFields_ThreadSafe_Cache |  1.178 ns | 0.0144 ns | 0.0128 ns | 41.56x faster |   0.79x |\n\n### `GetInterfaces()` 1,439% faster\n\n| Method                | Mean       | Error     | StdDev    | Ratio         | RatioSD |\n|---------------------- |-----------:|----------:|----------:|--------------:|--------:|\n| GetInterfaces_NoCache | 13.1880 ns | 0.1197 ns | 0.0999 ns |      baseline |         |\n| GetInterfaces_Cache   |  0.8649 ns | 0.0469 ns | 0.0439 ns | 15.39x faster |   0.58x |\n\n### `GetInterface()` 144% faster\n\n| Method               | Mean     | Error    | StdDev   | Ratio        | RatioSD |\n|--------------------- |---------:|---------:|---------:|-------------:|--------:|\n| GetInterface_NoCache | 32.84 ns | 0.411 ns | 0.364 ns |     baseline |         |\n| GetInterface_Cache   | 13.47 ns | 0.227 ns | 0.212 ns | 2.44x faster |   0.05x |\n\n### `GetConstructors()` 4,054% faster\n\n| Method                           | Mean       | Error     | StdDev    | Ratio         | RatioSD |\n|--------------------------------- |-----------:|----------:|----------:|--------------:|--------:|\n| GetConstructors_NoCache          | 38.4477 ns | 0.2020 ns | 0.1687 ns |      baseline |         |\n| GetConstructors_Cache            |  0.9280 ns | 0.0109 ns | 0.0102 ns | 41.54x faster |   0.36x |\n| GetConstructors_ThreadSafe_Cache |  1.0120 ns | 0.0689 ns | 0.0645 ns | 38.39x faster |   2.42x |\n\n### `GetConstructor()` 601% faster\n\n| Method                            | Mean      | Error    | StdDev   | Ratio        | RatioSD |\n|---------------------------------- |----------:|---------:|---------:|-------------:|--------:|\n| GetConstructor_NoCache            |  18.16 ns | 0.298 ns | 0.278 ns |     baseline |         |\n| GetConstructor_NoCache_Parameters | 127.18 ns | 1.592 ns | 1.489 ns | 7.01x slower |   0.16x |\n| GetConstructor_Cache              |  10.36 ns | 0.057 ns | 0.048 ns | 1.76x faster |   0.03x |\n| GetConstructor_Cache_Parameters   |  21.12 ns | 0.366 ns | 0.342 ns | 1.16x slower |   0.02x |\n\n### `Activator.CreateInstance(params)` vs `cachedConstructor.CreateInstance(params)` 242% faster\n\n| Method                               | Mean      | Error    | StdDev   | Ratio        | RatioSD |\n|------------------------------------- |----------:|---------:|---------:|-------------:|--------:|\n| Activator_Create_with_parameters     | 325.58 ns | 1.713 ns | 1.431 ns |     baseline |         |\n| Cache_CreateInstance_with_parameters |  95.61 ns | 1.943 ns | 1.908 ns | 3.42x faster |   0.07x |\n\n### `GetCustomAttributes()` 1,658% faster\n\n| Method                | Mean        | Error    | StdDev   | Ratio           | RatioSD |\n|---------------------- |------------:|---------:|---------:|----------------:|--------:|\n| GetAttributes_NoCache | 2,560.76 ns | 6.740 ns | 6.305 ns |        baseline |         |\n| GetAttributes_Cache   |    15.35 ns | 0.287 ns | 0.268 ns | 166.858x faster |   2.97x |\n\n### `GetGenericTypeDefinition()` 499% faster\n\n| Method                           | Mean      | Error     | StdDev    | Ratio        | RatioSD |\n|--------------------------------- |----------:|----------:|----------:|-------------:|--------:|\n| GetGenericTypeDefinition_NoCache | 1.8759 ns | 0.0481 ns | 0.0450 ns |     baseline |         |\n| GetGenericTypeDefinition_Cache   | 0.3159 ns | 0.0313 ns | 0.0293 ns | 5.99x faster |   0.58x |\n\n### `IsAssignableFrom()` 51% faster\n\n| Method                   | Mean     | Error     | StdDev    | Ratio        | RatioSD |\n|------------------------- |---------:|----------:|----------:|-------------:|--------:|\n| IsAssignableFrom_NoCache | 9.355 ns | 0.1357 ns | 0.1270 ns |     baseline |         |\n| IsAssignableFrom_Cache   | 6.198 ns | 0.0794 ns | 0.0742 ns | 1.51x faster |   0.04x |\n\n### `MakeGenericType()` 1,485% faster\n\n| Method                  | Mean      | Error    | StdDev   | Ratio         | RatioSD |\n|------------------------ |----------:|---------:|---------:|--------------:|--------:|\n| MakeGenericType_NoCache | 158.56 ns | 0.900 ns | 0.842 ns |      baseline |         |\n| MakeGenericType_Cache   |  10.01 ns | 0.216 ns | 0.202 ns | 15.85x faster |   0.31x |\n\n### `GetElementType()` 1,847% faster\n\n| Method                 | Mean      | Error     | StdDev    | Ratio         | RatioSD |\n|----------------------- |----------:|----------:|----------:|--------------:|--------:|\n| GetElementType_NoCache | 4.9175 ns | 0.0442 ns | 0.0369 ns |      baseline |         |\n| GetElementType_Cache   | 0.2585 ns | 0.0204 ns | 0.0191 ns | 19.47x faster |   1.06x |\n\n## Properties on `Type` (e.g. `typeof(string).IsNullable`)\n\n| Method                | Mean      | Error     | StdDev    | Median    |\n|---------------------- |----------:|----------:|----------:|----------:|\n| IsAbstract_NoCache    | 2.4277 ns | 0.0319 ns | 0.0299 ns | 2.4152 ns |\n| IsAbstract_Cache      | 0.0000 ns | 0.0000 ns | 0.0000 ns | 0.0000 ns |\n| IsInterface_NoCache   | 0.7720 ns | 0.0219 ns | 0.0194 ns | 0.7662 ns |\n| IsInterface_Cache     | 0.0045 ns | 0.0076 ns | 0.0071 ns | 0.0000 ns |\n| IsGenericType_NoCache | 0.8707 ns | 0.0281 ns | 0.0262 ns | 0.8732 ns |\n| IsGenericType_Cache   | 0.2667 ns | 0.0195 ns | 0.0182 ns | 0.2651 ns |\n| IsEnum_NoCache        | 0.5322 ns | 0.0242 ns | 0.0227 ns | 0.5291 ns |\n| IsEnum_Cache          | 0.2612 ns | 0.0251 ns | 0.0235 ns | 0.2526 ns |\n| IsNullable_NoCache    | 1.4296 ns | 0.0297 ns | 0.0278 ns | 1.4305 ns |\n| IsNullable_Cache      | 0.2312 ns | 0.0077 ns | 0.0065 ns | 0.2278 ns |\n| IsByRef_NoCache       | 1.8439 ns | 0.0332 ns | 0.0310 ns | 1.8410 ns |\n| IsByRef_Cache         | 0.0012 ns | 0.0025 ns | 0.0022 ns | 0.0000 ns |\n| IsArray_NoCache       | 2.2914 ns | 0.0484 ns | 0.0452 ns | 2.2652 ns |\n| IsArray_Cache         | 0.0060 ns | 0.0102 ns | 0.0096 ns | 0.0000 ns |\n\nNotes:\n- These benchmarks are built over iterations. The first operation is going to be as slow as the Reflection it sits in front of.\n- Many of these are based off of a test class `TestType` which is located in the test library.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoenneker%2Fsoenneker.reflection.cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoenneker%2Fsoenneker.reflection.cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoenneker%2Fsoenneker.reflection.cache/lists"}