{"id":3579,"date":"2025-04-19T10:31:43","date_gmt":"2025-04-19T02:31:43","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3579"},"modified":"2025-04-19T10:31:43","modified_gmt":"2025-04-19T02:31:43","slug":"%e9%85%8d%e5%90%88%e5%8f%8d%e5%b0%84%e5%8a%a8%e6%80%81%e8%ae%bf%e9%97%ae%e7%b4%a2%e5%bc%95%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/19\/%e9%85%8d%e5%90%88%e5%8f%8d%e5%b0%84%e5%8a%a8%e6%80%81%e8%ae%bf%e9%97%ae%e7%b4%a2%e5%bc%95%e5%99%a8\/","title":{"rendered":"\u914d\u5408\u53cd\u5c04\u52a8\u6001\u8bbf\u95ee\u7d22\u5f15\u5668"},"content":{"rendered":"\n<p>\u5728 C# \u4e2d\uff0c<strong>\u53cd\u5c04<\/strong>\uff08Reflection\uff09\u53ef\u4ee5\u7528\u4e8e\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u8bbf\u95ee\u5bf9\u8c61\u7684\u6210\u5458\uff08\u5982\u5b57\u6bb5\u3001\u5c5e\u6027\u3001\u65b9\u6cd5\u7b49\uff09\uff0c\u5305\u62ec<strong>\u7d22\u5f15\u5668<\/strong>\u3002\u7ed3\u5408 <strong>\u53cd\u5c04<\/strong> \u548c <strong>\u7d22\u5f15\u5668<\/strong> \u53ef\u4ee5\u5728\u4e0d\u63d0\u524d\u77e5\u9053\u7c7b\u6216\u5bf9\u8c61\u7684\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u7d22\u5f15\u5668\u52a8\u6001\u8bbf\u95ee\u548c\u4fee\u6539\u5bf9\u8c61\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4f7f\u7528\u573a\u666f\uff1a<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u52a8\u6001\u6570\u636e\u8bbf\u95ee<\/strong>\uff1a\u5f53\u4f60\u9700\u8981\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u8bbf\u95ee\u67d0\u4e9b\u5bf9\u8c61\u7684\u5c5e\u6027\u6216\u7d22\u5f15\u5668\u65f6\uff0c\u53cd\u5c04\u4e0e\u7d22\u5f15\u5668\u7ed3\u5408\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u53cd\u5c04\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u8fd0\u884c\u65f6\u51b3\u5b9a\u54ea\u4e2a\u7d22\u5f15\u5668\u9700\u8981\u88ab\u8c03\u7528\u3002<\/li>\n\n\n\n<li><strong>\u6784\u5efa\u901a\u7528\u5de5\u5177\u6216\u6846\u67b6<\/strong>\uff1a\u53cd\u5c04\u5141\u8bb8\u6211\u4eec\u6784\u5efa\u6846\u67b6\u6216\u5de5\u5177\uff0c\u53ef\u4ee5\u52a8\u6001\u8bfb\u53d6\u6216\u4fee\u6539\u5bf9\u8c61\u7684\u6570\u636e\uff08\u4f8b\u5982\uff0cORM \u6846\u67b6\u3001\u6570\u636e\u7ed1\u5b9a\u6846\u67b6\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u76ee\u6807\uff1a\u4f7f\u7528\u53cd\u5c04\u52a8\u6001\u8bbf\u95ee\u548c\u4fee\u6539\u7d22\u5f15\u5668<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u7d22\u5f15\u5668\u7684\u7c7b<\/strong><\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7d22\u5f15\u5668\u7684\u7c7b\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u53cd\u5c04\u52a8\u6001\u8bbf\u95ee\u8be5\u7d22\u5f15\u5668\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Reflection;\nusing System.Collections.Generic;\n\npublic class Config\n{\n    private Dictionary&lt;string, string&gt; _settings = new Dictionary&lt;string, string&gt;();\n\n    \/\/ \u7d22\u5f15\u5668\uff1a\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u914d\u7f6e\u9879\n    public string this&#91;string key]\n    {\n        get =&gt; _settings.ContainsKey(key) ? _settings&#91;key] : null;\n        set =&gt; _settings&#91;key] = value;\n    }\n\n    public Config()\n    {\n        \/\/ \u521d\u59cb\u5316\u4e00\u4e9b\u9ed8\u8ba4\u914d\u7f6e\n        _settings&#91;\"AppVersion\"] = \"1.0.0\";\n        _settings&#91;\"ApiUrl\"] = \"https:\/\/api.example.com\";\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u901a\u8fc7\u53cd\u5c04\u52a8\u6001\u8bbf\u95ee\u7d22\u5f15\u5668<\/strong><\/h3>\n\n\n\n<p>\u4f7f\u7528\u53cd\u5c04\u53ef\u4ee5\u52a8\u6001\u5730\u83b7\u53d6\u7c7b\u7684\u7c7b\u578b\u4fe1\u606f\uff0c\u8fdb\u800c\u8bbf\u95ee\u5176\u7d22\u5f15\u5668\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\npublic class ReflectionExample\n{\n    public static void Main()\n    {\n        var config = new Config();\n\n        \/\/ \u83b7\u53d6Config\u7c7b\u578b\u7684\u53cd\u5c04\u4fe1\u606f\n        Type configType = config.GetType();\n\n        \/\/ \u83b7\u53d6\u7d22\u5f15\u5668\u5c5e\u6027\n        PropertyInfo indexerProperty = configType.GetProperty(\"Item\");\n\n        \/\/ \u8c03\u7528\u7d22\u5f15\u5668\uff1a\u901a\u8fc7\u7d22\u5f15\u5668\u952e \"ApiUrl\" \u83b7\u53d6\u914d\u7f6e\u503c\n        object value = indexerProperty.GetValue(config, new object&#91;] { \"ApiUrl\" });\n        Console.WriteLine($\"ApiUrl: {value}\");  \/\/ \u8f93\u51fa\uff1aApiUrl: https:\/\/api.example.com\n\n        \/\/ \u901a\u8fc7\u53cd\u5c04\u8bbe\u7f6e\u7d22\u5f15\u5668\uff1a\u4fee\u6539\u914d\u7f6e\n        indexerProperty.SetValue(config, \"https:\/\/newapi.example.com\", new object&#91;] { \"ApiUrl\" });\n\n        \/\/ \u9a8c\u8bc1\u4fee\u6539\n        value = indexerProperty.GetValue(config, new object&#91;] { \"ApiUrl\" });\n        Console.WriteLine($\"Updated ApiUrl: {value}\");  \/\/ \u8f93\u51fa\uff1aUpdated ApiUrl: https:\/\/newapi.example.com\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u53cd\u5c04\u4e2d\u4f7f\u7528\u7d22\u5f15\u5668\u7684\u5173\u952e\u6b65\u9aa4<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u83b7\u53d6\u7d22\u5f15\u5668\u5c5e\u6027<\/strong>\uff1a\u4f7f\u7528 <code>Type.GetProperty(\"Item\")<\/code> \u83b7\u53d6\u7d22\u5f15\u5668\u7684\u53cd\u5c04\u4fe1\u606f\u3002\u5728 C# \u4e2d\uff0c\u7d22\u5f15\u5668\u662f\u901a\u8fc7 <code>Item<\/code> \u4f5c\u4e3a\u5c5e\u6027\u540d\u6765\u8868\u793a\u7684\u3002<\/li>\n\n\n\n<li><strong>\u8c03\u7528\u7d22\u5f15\u5668\u7684 <code>GetValue<\/code> \u548c <code>SetValue<\/code> \u65b9\u6cd5<\/strong>\uff1a\u901a\u8fc7\u53cd\u5c04\u8c03\u7528\u7d22\u5f15\u5668\u65f6\uff0c\u6211\u4eec\u4f20\u9012\u53c2\u6570 <code>new object[] { \"ApiUrl\" }<\/code> \u6765\u6307\u5b9a\u7d22\u5f15\u5668\u7684\u952e\uff08key\uff09\u3002<code>GetValue<\/code> \u7528\u6765\u83b7\u53d6\u503c\uff0c<code>SetValue<\/code> \u7528\u6765\u4fee\u6539\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u91cd\u8981\u6ce8\u610f\u4e8b\u9879\uff1a<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7d22\u5f15\u5668\u662f\u7279\u6b8a\u7684\u5c5e\u6027<\/strong>\uff1a\u5728\u53cd\u5c04\u4e2d\uff0c\u7d22\u5f15\u5668\u662f\u901a\u8fc7 <code>Item<\/code> \u4f5c\u4e3a\u5c5e\u6027\u540d\u6765\u8bbf\u95ee\u7684\u3002\u5c3d\u7ba1\u7d22\u5f15\u5668\u662f\u65b9\u6cd5\uff0c\u4f46\u5728\u53cd\u5c04\u65f6\u5b83\u4eec\u88ab\u89c6\u4f5c\u5c5e\u6027\u3002<\/li>\n\n\n\n<li><strong>\u53c2\u6570\u4f20\u9012<\/strong>\uff1a\u5728\u8c03\u7528\u7d22\u5f15\u5668\u65f6\uff0c\u5fc5\u987b\u4f20\u9012\u53c2\u6570\uff08\u7d22\u5f15\u952e\uff09\uff0c\u5373 <code>SetValue<\/code> \u548c <code>GetValue<\/code> \u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u5305\u542b\u7d22\u5f15\u5668\u53c2\u6570\u7684\u6570\u7ec4\u3002<\/li>\n\n\n\n<li><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u53cd\u5c04\u901a\u5e38\u6bd4\u76f4\u63a5\u8bbf\u95ee\u8981\u6162\uff0c\u6240\u4ee5\u5728\u9ad8\u9891\u6b21\u7684\u64cd\u4f5c\u4e2d\u5e94\u8be5\u5c0f\u5fc3\u4f7f\u7528\u53cd\u5c04\u3002\u5982\u679c\u6027\u80fd\u662f\u4e00\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u7f13\u5b58\u53cd\u5c04\u4fe1\u606f\u6216\u907f\u514d\u9891\u7e41\u7684\u53cd\u5c04\u8c03\u7528\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u53cd\u5c04\u4e0e\u7d22\u5f15\u5668\u7ed3\u5408\u7684\u5b9e\u9645\u5e94\u7528<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\uff1a\u901a\u8fc7\u53cd\u5c04\u548c\u7d22\u5f15\u5668\u8bbf\u95ee\u5bf9\u8c61\u6570\u636e<\/h4>\n\n\n\n<p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u77e5\u9053\u76ee\u6807\u5bf9\u8c61\u7684\u7c7b\u578b\u6216\u8005\u7d22\u5f15\u5668\u7684\u952e\u3002\u4f8b\u5982\uff0c\u5728\u5f00\u53d1\u4e00\u4e9b\u901a\u7528\u7684\u6846\u67b6\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u7528\u53cd\u5c04\u52a8\u6001\u5904\u7406\u6765\u81ea\u4e0d\u540c\u7c7b\u7684\u6570\u636e\u3002\u8fd9\u65f6\uff0c\u53cd\u5c04\u548c\u7d22\u5f15\u5668\u7ed3\u5408\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u79cd\u52a8\u6001\u8bbf\u95ee\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Reflection;\nusing System.Collections.Generic;\n\npublic class DynamicAccessor\n{\n    public static void SetPropertyValue(object obj, string propertyName, object value)\n    {\n        Type type = obj.GetType();\n\n        \/\/ \u67e5\u627e\u7d22\u5f15\u5668\u5c5e\u6027\n        PropertyInfo propertyInfo = type.GetProperty(\"Item\");\n\n        if (propertyInfo != null)\n        {\n            \/\/ \u4f7f\u7528\u7d22\u5f15\u5668\u8bbe\u7f6e\u5c5e\u6027\u503c\n            propertyInfo.SetValue(obj, value, new object&#91;] { propertyName });\n        }\n    }\n\n    public static object GetPropertyValue(object obj, string propertyName)\n    {\n        Type type = obj.GetType();\n\n        \/\/ \u67e5\u627e\u7d22\u5f15\u5668\u5c5e\u6027\n        PropertyInfo propertyInfo = type.GetProperty(\"Item\");\n\n        if (propertyInfo != null)\n        {\n            \/\/ \u4f7f\u7528\u7d22\u5f15\u5668\u83b7\u53d6\u5c5e\u6027\u503c\n            return propertyInfo.GetValue(obj, new object&#91;] { propertyName });\n        }\n\n        return null;\n    }\n}\n\npublic class Config\n{\n    private Dictionary&lt;string, string&gt; _settings = new Dictionary&lt;string, string&gt;();\n\n    public string this&#91;string key]\n    {\n        get =&gt; _settings.ContainsKey(key) ? _settings&#91;key] : null;\n        set =&gt; _settings&#91;key] = value;\n    }\n\n    public Config()\n    {\n        _settings&#91;\"AppVersion\"] = \"1.0.0\";\n        _settings&#91;\"ApiUrl\"] = \"https:\/\/api.example.com\";\n    }\n}\n\npublic class Program\n{\n    public static void Main()\n    {\n        var config = new Config();\n\n        \/\/ \u901a\u8fc7\u53cd\u5c04\u52a8\u6001\u8bbe\u7f6e\u548c\u83b7\u53d6\u914d\u7f6e\n        Console.WriteLine(DynamicAccessor.GetPropertyValue(config, \"ApiUrl\"));  \/\/ \u8f93\u51fa\uff1ahttps:\/\/api.example.com\n        DynamicAccessor.SetPropertyValue(config, \"ApiUrl\", \"https:\/\/newapi.example.com\");\n        Console.WriteLine(DynamicAccessor.GetPropertyValue(config, \"ApiUrl\"));  \/\/ \u8f93\u51fa\uff1ahttps:\/\/newapi.example.com\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c0f\u7ed3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7d22\u5f15\u5668\u4e0e\u53cd\u5c04\u7ed3\u5408<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u5f97\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u52a8\u6001\u548c\u7075\u6d3b\uff0c\u5c24\u5176\u5728\u5904\u7406\u52a8\u6001\u6570\u636e\u3001\u6846\u67b6\u8bbe\u8ba1\u3001\u63d2\u4ef6\u673a\u5236\u7b49\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/li>\n\n\n\n<li><strong>\u53cd\u5c04\u7684\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u867d\u7136\u7ed3\u5408\u7d22\u5f15\u5668\u548c\u53cd\u5c04\u63d0\u4f9b\u4e86\u9ad8\u5ea6\u7684\u7075\u6d3b\u6027\uff0c\u4f46\u53cd\u5c04\u7684\u6027\u80fd\u5f00\u9500\u8f83\u5927\uff0c\u56e0\u6b64\u9700\u8981\u5728\u6027\u80fd\u654f\u611f\u7684\u573a\u5408\u8c28\u614e\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong>\u5e94\u7528\u573a\u666f<\/strong>\uff1a\u53cd\u5c04\u548c\u7d22\u5f15\u5668\u7684\u7ed3\u5408\u53ef\u4ee5\u5e94\u7528\u4e8e\u914d\u7f6e\u7ba1\u7406\u3001\u7f13\u5b58\u5c01\u88c5\u3001JSON \u6620\u5c04\u3001\u52a8\u6001\u5bf9\u8c61\u8bbf\u95ee\u7b49\u9886\u57df\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7f16\u7a0b\u81ea\u7531\u5ea6\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5e0c\u671b\u8fd9\u4e2a\u793a\u4f8b\u548c\u5206\u6790\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728 C# \u4e2d\u7ed3\u5408\u53cd\u5c04\u548c\u7d22\u5f15\u5668\u6765\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C# \u4e2d\uff0c\u53cd\u5c04\uff08Reflection\uff09\u53ef\u4ee5\u7528\u4e8e\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u8bbf\u95ee\u5bf9\u8c61\u7684\u6210\u5458\uff08\u5982\u5b57\u6bb5\u3001\u5c5e\u6027\u3001\u65b9\u6cd5\u7b49\uff09\uff0c\u5305\u62ec\u7d22\u5f15 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3580,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3579","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\/3579","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=3579"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3579\/revisions"}],"predecessor-version":[{"id":3581,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3579\/revisions\/3581"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3580"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}