{"id":3576,"date":"2025-04-19T10:28:14","date_gmt":"2025-04-19T02:28:14","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3576"},"modified":"2025-04-19T10:28:14","modified_gmt":"2025-04-19T02:28:14","slug":"%e5%ae%9e%e9%99%85%e9%a1%b9%e7%9b%ae%e4%b8%ad%e7%b4%a2%e5%bc%95%e5%99%a8%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/19\/%e5%ae%9e%e9%99%85%e9%a1%b9%e7%9b%ae%e4%b8%ad%e7%b4%a2%e5%bc%95%e5%99%a8%e7%9a%84%e4%bd%bf%e7%94%a8\/","title":{"rendered":"\u5b9e\u9645\u9879\u76ee\u4e2d\u7d22\u5f15\u5668\u7684\u4f7f\u7528"},"content":{"rendered":"\n<p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c<strong>\u7d22\u5f15\u5668<\/strong>\uff08Indexer\uff09\u901a\u5e38\u7528\u4e8e\u7b80\u5316\u5bf9\u6570\u636e\u7684\u8bbf\u95ee\uff0c\u5c24\u5176\u662f\u5728\u6d89\u53ca<strong>\u96c6\u5408\u7c7b<\/strong>\u3001<strong>\u914d\u7f6e\u7ba1\u7406<\/strong>\u3001<strong>\u7f13\u5b58\u5c01\u88c5<\/strong>\u4ee5\u53ca<strong>JSON \u6620\u5c04<\/strong>\u7b49\u573a\u666f\u4e2d\uff0c\u80fd\u663e\u8457\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7075\u6d3b\u6027\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5b9e\u9645\u9879\u76ee\u4e2d\u4f7f\u7528\u7d22\u5f15\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>JSON \u6620\u5c04<\/strong><\/h2>\n\n\n\n<p>\u5728\u5904\u7406 JSON \u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u5668\u6765\u5c01\u88c5 JSON \u5bf9\u8c61\u7684\u8bbf\u95ee\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u4e14\u5177\u6709\u826f\u597d\u7684\u6269\u5c55\u6027\u3002\u5e38\u89c1\u7684\u7528\u9014\u662f\u5c06 JSON \u5b57\u7b26\u4e32\u6620\u5c04\u4e3a\u952e\u503c\u5bf9\uff0c\u5e76\u901a\u8fc7\u7d22\u5f15\u5668\u8bbf\u95ee\u5177\u4f53\u7684\u5b57\u6bb5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf \u793a\u4f8b\uff1aJSON \u6620\u5c04\u5668<\/h3>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a JSON \u6570\u636e\uff0c\u8868\u793a\u67d0\u4e9b\u914d\u7f6e\u9879\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"Name\": \"ChatGPT\",\n    \"Version\": \"1.0\",\n    \"Enabled\": true\n}<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7c7b\u6765\u89e3\u6790\u5e76\u901a\u8fc7\u7d22\u5f15\u5668\u8bbf\u95ee JSON \u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using Newtonsoft.Json;\nusing System.Collections.Generic;\n\npublic class JsonMapper\n{\n    private readonly Dictionary&lt;string, object&gt; _data;\n\n    public JsonMapper(string json)\n    {\n        _data = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, object&gt;&gt;(json);\n    }\n\n    \/\/ \u901a\u8fc7\u7d22\u5f15\u5668\u63d0\u4f9b JSON \u6570\u636e\u7684\u8bbf\u95ee\n    public object this&#91;string key]\n    {\n        get =&gt; _data.ContainsKey(key) ? _data&#91;key] : null;\n        set =&gt; _data&#91;key] = value;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4f7f\u7528\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>string json = \"{\\\"Name\\\": \\\"ChatGPT\\\", \\\"Version\\\": \\\"1.0\\\", \\\"Enabled\\\": true}\";\nvar mapper = new JsonMapper(json);\n\nConsole.WriteLine(mapper&#91;\"Name\"]); \/\/ \u8f93\u51fa\uff1aChatGPT\nConsole.WriteLine(mapper&#91;\"Version\"]); \/\/ \u8f93\u51fa\uff1a1.0\nmapper&#91;\"Enabled\"] = false; \/\/ \u4fee\u6539\u503c\nConsole.WriteLine(mapper&#91;\"Enabled\"]); \/\/ \u8f93\u51fa\uff1aFalse<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u5c0f\u8d34\u58eb\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u7d22\u5f15\u5668\u5c01\u88c5 JSON \u6570\u636e\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u7279\u5b9a\u7684\u5b57\u6bb5\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7 <code>Dictionary&lt;string, object><\/code> \u53ef\u4ee5\u8f7b\u677e\u5730\u5c06 JSON \u952e\u503c\u5bf9\u6620\u5c04\u5230\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>\u914d\u7f6e\u8bbf\u95ee\u5668<\/strong><\/h2>\n\n\n\n<p>\u5728\u4f01\u4e1a\u7ea7\u5e94\u7528\u4e2d\uff0c\u914d\u7f6e\u901a\u5e38\u5b58\u50a8\u5728\u6587\u4ef6\uff08\u5982 JSON\u3001XML\u3001YAML\uff09\u6216\u6570\u636e\u5e93\u4e2d\u3002\u901a\u8fc7\u7d22\u5f15\u5668\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u914d\u7f6e\u9879\uff0c\u540c\u65f6\u4fdd\u6301\u7075\u6d3b\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf \u793a\u4f8b\uff1a\u914d\u7f6e\u7ba1\u7406\u5668<\/h3>\n\n\n\n<p>\u5047\u8bbe\u914d\u7f6e\u5b58\u50a8\u5728\u4e00\u4e2a JSON \u6587\u4ef6\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"AppSettings\": {\n        \"ApiUrl\": \"https:\/\/api.example.com\",\n        \"Timeout\": 30\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u7ba1\u7406\u5668\u7c7b\uff0c\u4f7f\u7528\u7d22\u5f15\u5668\u8bbf\u95ee\u7279\u5b9a\u7684\u914d\u7f6e\u9879\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using Newtonsoft.Json.Linq;\n\npublic class ConfigManager\n{\n    private readonly JObject _config;\n\n    public ConfigManager(string json)\n    {\n        _config = JObject.Parse(json);\n    }\n\n    \/\/ \u4f7f\u7528\u7d22\u5f15\u5668\u6765\u8bbf\u95ee\u5d4c\u5957\u7684\u914d\u7f6e\u9879\n    public string this&#91;string section, string key]\n    {\n        get =&gt; _config&#91;section]?&#91;key]?.ToString();\n        set\n        {\n            if (_config&#91;section] == null)\n            {\n                _config&#91;section] = new JObject();\n            }\n            _config&#91;section]&#91;key] = value;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4f7f\u7528\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>string configJson = \"{ \\\"AppSettings\\\": { \\\"ApiUrl\\\": \\\"https:\/\/api.example.com\\\", \\\"Timeout\\\": 30 }}\";\nvar configManager = new ConfigManager(configJson);\n\nConsole.WriteLine(configManager&#91;\"AppSettings\", \"ApiUrl\"]);  \/\/ \u8f93\u51fa\uff1ahttps:\/\/api.example.com\nConsole.WriteLine(configManager&#91;\"AppSettings\", \"Timeout\"]); \/\/ \u8f93\u51fa\uff1a30\n\n\/\/ \u4fee\u6539\u914d\u7f6e\u9879\nconfigManager&#91;\"AppSettings\", \"Timeout\"] = \"60\";\nConsole.WriteLine(configManager&#91;\"AppSettings\", \"Timeout\"]); \/\/ \u8f93\u51fa\uff1a60<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u5c0f\u8d34\u58eb\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7\u7d22\u5f15\u5668\uff0c\u80fd\u591f\u7b80\u5316\u5bf9\u914d\u7f6e\u6570\u636e\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/li>\n\n\n\n<li>\u652f\u6301\u5d4c\u5957\u914d\u7f6e\u7684\u8bbf\u95ee\uff0c\u4f7f\u5f97\u914d\u7f6e\u7ed3\u6784\u5c42\u7ea7\u5316\u548c\u53ef\u6269\u5c55\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>\u7f13\u5b58\u8bbf\u95ee\u5c01\u88c5<\/strong><\/h2>\n\n\n\n<p>\u5728\u7f13\u5b58\u7ba1\u7406\u4e2d\uff0c\u7d22\u5f15\u5668\u7528\u4e8e\u5c06\u6570\u636e\u5c01\u88c5\u8fdb\u7f13\u5b58\uff0c\u5e76\u901a\u8fc7 <code>[]<\/code> \u64cd\u4f5c\u7b26\u7b80\u6d01\u5730\u8fdb\u884c\u5b58\u53d6\u3002\u5e38\u89c1\u7684\u7f13\u5b58\u573a\u666f\u5305\u62ec\u5185\u5b58\u7f13\u5b58\u3001\u5206\u5e03\u5f0f\u7f13\u5b58\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf \u793a\u4f8b\uff1a\u7b80\u5355\u7684\u5185\u5b58\u7f13\u5b58<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\n\npublic class MemoryCache\n{\n    private readonly Dictionary&lt;string, object&gt; _cache = new Dictionary&lt;string, object&gt;();\n\n    \/\/ \u4f7f\u7528\u7d22\u5f15\u5668\u5c01\u88c5\u7f13\u5b58\u5b58\u53d6\n    public object this&#91;string key]\n    {\n        get =&gt; _cache.ContainsKey(key) ? _cache&#91;key] : null;\n        set =&gt; _cache&#91;key] = value;\n    }\n\n    public bool ContainsKey(string key) =&gt; _cache.ContainsKey(key);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4f7f\u7528\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var cache = new MemoryCache();\n\n\/\/ \u5b58\u5165\u7f13\u5b58\ncache&#91;\"user:1234\"] = new { Name = \"John Doe\", Age = 30 };\n\n\/\/ \u83b7\u53d6\u7f13\u5b58\nvar user = cache&#91;\"user:1234\"];\nif (user != null)\n{\n    Console.WriteLine(user.Name);  \/\/ \u8f93\u51fa\uff1aJohn Doe\n}\nelse\n{\n    Console.WriteLine(\"Cache miss.\");\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u5c0f\u8d34\u58eb\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7d22\u5f15\u5668\u4f7f\u5f97\u7f13\u5b58\u5b58\u53d6\u53d8\u5f97\u975e\u5e38\u76f4\u89c2\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u6269\u5c55\u7f13\u5b58\u903b\u8f91\uff0c\u652f\u6301\u8fc7\u671f\u65f6\u95f4\u3001\u5931\u6548\u7b56\u7565\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>\u9ad8\u7ea7\u7528\u6cd5\uff1a\u7ed3\u5408\u7d22\u5f15\u5668\u4e0e\u5b57\u5178\/\u96c6\u5408\u7c7b<\/strong><\/h2>\n\n\n\n<p>\u5728\u5904\u7406\u7c7b\u4f3c JSON \u6620\u5c04\u548c\u914d\u7f6e\u8bbf\u95ee\u7b49\u573a\u666f\u65f6\uff0c\u5e38\u5e38\u7ed3\u5408\u4f7f\u7528\u7d22\u5f15\u5668\u4e0e\u96c6\u5408\u7c7b\uff08\u5982 <code>Dictionary&lt;string, object&gt;<\/code>\u3001<code>List&lt;T&gt;<\/code>\uff09\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u7d22\u5f15\u5668\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e76\u5728\u591a\u79cd\u60c5\u51b5\u4e0b\u7b80\u5316\u6570\u636e\u8bbf\u95ee\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf \u793a\u4f8b\uff1a\u52a8\u6001\u914d\u7f6e\u7ba1\u7406<\/h3>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u7684\u914d\u7f6e\u6570\u636e\u5305\u542b\u591a\u4e2a\u5b50\u914d\u7f6e\u9879\uff0c\u4f7f\u7528 <code>Dictionary&lt;string, object&gt;<\/code> \u6765\u8868\u793a\u6bcf\u4e2a\u914d\u7f6e\u9879\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DynamicConfig\n{\n    private readonly Dictionary&lt;string, object&gt; _config = new();\n\n    public object this&#91;string key]\n    {\n        get =&gt; _config.ContainsKey(key) ? _config&#91;key] : null;\n        set =&gt; _config&#91;key] = value;\n    }\n\n    public bool TryGetValue(string key, out object value)\n    {\n        return _config.TryGetValue(key, out value);\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4f7f\u7528\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var dynamicConfig = new DynamicConfig();\ndynamicConfig&#91;\"ConnectionString\"] = \"Server=localhost;Database=mydb\";\ndynamicConfig&#91;\"AppVersion\"] = \"1.0.0\";\n\n\/\/ \u83b7\u53d6\u914d\u7f6e\u9879\nConsole.WriteLine(dynamicConfig&#91;\"ConnectionString\"]); \/\/ \u8f93\u51fa\uff1aServer=localhost;Database=mydb\nConsole.WriteLine(dynamicConfig&#91;\"AppVersion\"]); \/\/ \u8f93\u51fa\uff1a1.0.0<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u5c0f\u8d34\u58eb\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u914d\u7f6e\u6570\u636e\uff0c\u53ef\u4ee5\u975e\u5e38\u7075\u6d3b\u5730\u7ba1\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u914d\u7f6e\u9879\u3002<\/li>\n\n\n\n<li>\u7d22\u5f15\u5668\u63d0\u4f9b\u4e86\u7b80\u6d01\u3001\u6613\u4e8e\u7ef4\u62a4\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u52a8\u6001\u914d\u7f6e\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcda \u6743\u5a01\u53c2\u8003<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/indexers\/\">C# Indexers &#8211; Microsoft Learn<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.newtonsoft.com\/json\">JSON.NET Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.collections.generic.dictionary-2\">Dictionary &#8211; Microsoft Learn<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c0f\u7ed3\uff1a<\/h3>\n\n\n\n<p>\u901a\u8fc7\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u7d22\u5f15\u5668\uff0c\u53ef\u4ee5\u5728 JSON \u6620\u5c04\u3001\u914d\u7f6e\u7ba1\u7406\u548c\u7f13\u5b58\u5c01\u88c5\u7b49\u573a\u666f\u4e2d\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u7d22\u5f15\u5668\u4e0d\u4ec5\u80fd\u591f\u63d0\u9ad8\u6570\u636e\u8bbf\u95ee\u7684\u6548\u7387\uff0c\u8fd8\u80fd\u8ba9\u4ee3\u7801\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u6269\u5c55\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u7d22\u5f15\u5668\uff08Indexer\uff09\u901a\u5e38\u7528\u4e8e\u7b80\u5316\u5bf9\u6570\u636e\u7684\u8bbf\u95ee\uff0c\u5c24\u5176\u662f\u5728\u6d89\u53ca\u96c6\u5408\u7c7b\u3001\u914d\u7f6e\u7ba1\u7406\u3001\u7f13\u5b58\u5c01\u88c5\u4ee5\u53caJS [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3577,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3576","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3576"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3576\/revisions"}],"predecessor-version":[{"id":3578,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3576\/revisions\/3578"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3577"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}