{"id":19029292,"url":"https://github.com/sulmar/dynamic-property","last_synced_at":"2026-07-14T18:33:13.927Z","repository":{"id":94110024,"uuid":"262022861","full_name":"sulmar/dynamic-property","owner":"sulmar","description":"Dynamiczne właściwości","archived":false,"fork":false,"pushed_at":"2020-05-07T11:12:47.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-25T13:59:40.981Z","etag":null,"topics":["csharp","reflection"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sulmar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-07T10:43:41.000Z","updated_at":"2020-05-07T11:12:49.000Z","dependencies_parsed_at":"2023-03-13T17:07:18.687Z","dependency_job_id":null,"html_url":"https://github.com/sulmar/dynamic-property","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sulmar/dynamic-property","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sulmar%2Fdynamic-property","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sulmar%2Fdynamic-property/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sulmar%2Fdynamic-property/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sulmar%2Fdynamic-property/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sulmar","download_url":"https://codeload.github.com/sulmar/dynamic-property/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sulmar%2Fdynamic-property/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35474801,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-14T02:00:06.603Z","response_time":114,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["csharp","reflection"],"created_at":"2024-11-08T21:13:47.507Z","updated_at":"2026-07-14T18:33:13.911Z","avatar_url":"https://github.com/sulmar.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n## Wprowadzenie\n\nW jednym z projektów postanowiłem ustawiać wartości w sposób dynamiczny, czyli po nazwie właściwości w Runtimie.\n\nZainspirowany konstrukcją z języka Javascript postanowiłem zaimplementować to w C#\n\nCzyli zamiast:\n\n~~~ csharp\ncustomer.FirstName = \"John\";\n\nConsole.WriteLine(customer.FirstName);\n~~~\n\nchciałem uzyskać coś takiego:\n\n~~~ csharp\n\ncustomer[\"FirstName\"] = \"John\";\n\nConsole.WriteLine(customer[\"FirstName\"]);\n\n~~~\n\n## Rozwiązanie z użyciem System.Reflection\n\nUtworzyłem własny indekser i użyłem refleksji.\n\n~~~ csharp\npublic class Customer\n{\n    public string FirstName { get; set; }\n    public string LastName { get; set; }\n    public string Email { get; set; }\n\n    public object this[string propertyName]\n    {\n        get =\u003e GetType().GetProperty(propertyName).GetValue(this);\n        set =\u003e GetType().GetProperty(propertyName).SetValue(this, value, null);\n    }\n}\n~~~\n\n\n## Rozwiązanie z użyciem FastMember\n\nUtworzyłem własny indekser i użyłem biblioteki **FastMember**.\n\n~~~\n dotnet add package FastMember\n~~~\n\n\n### ObjectAccessor\n~~~ csharp\n\n  public class Customer\n    {\n        public string FirstName { get; set; }\n        public string LastName { get; set; }\n        public string Email { get; set; }\n\n        private readonly ObjectAccessor wrapper;\n\n        public Customer()\n        {\n            wrapper = ObjectAccessor.Create(this);\n        }\n\n        public object this[string propertyName]\n        {\n            get =\u003e wrapper[propertyName];\n            set =\u003e wrapper[propertyName] = value;\n        }\n    }\n\n~~~\n\n\n### TypeAccessor\n\n~~~ csharp\n\n public class Customer\n    {\n        public string FirstName { get; set; }\n        public string LastName { get; set; }\n        public string Email { get; set; }\n\n        private readonly TypeAccessor accessor;\n\n        public Customer()\n        {\n            accessor = TypeAccessor.Create(GetType());\n        }\n\n        public object this[string propertyName]\n        {\n            get =\u003e accessor[this, propertyName];\n            set =\u003e accessor[this, propertyName] = value;\n        }\n    }\n\n~~~\n\n## Benchmarks\n\nPorównanie rozwiązań:\n\n\n|                   Method |      Mean |    Error |   StdDev | Rank |\n|------------------------- |----------:|---------:|---------:|-----:|\n|   FastMemberTypeAccessor |  62.96 ns | 0.927 ns | 0.822 ns |    1 |\n| FastMemberObjectAccessor |  82.74 ns | 1.141 ns | 1.068 ns |    2 |\n|               Reflection | 243.04 ns | 4.884 ns | 8.294 ns |    3 |\n\n## Podsumowanie\n\nJak widać zwyciezcą został FastMember, zwłaszcza metoda FastMemberTypeAccessor i takie rozwiązanie polecam.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsulmar%2Fdynamic-property","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsulmar%2Fdynamic-property","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsulmar%2Fdynamic-property/lists"}