{"id":3565,"date":"2025-04-19T10:12:52","date_gmt":"2025-04-19T02:12:52","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3565"},"modified":"2025-04-19T10:12:52","modified_gmt":"2025-04-19T02:12:52","slug":"%e5%b1%9e%e6%80%a7%ef%bc%88property%ef%bc%89-%e5%8f%8d%e5%b0%84%ef%bc%88reflection%ef%bc%89-%e7%9a%84%e5%ae%9e%e6%88%98%e6%a1%88%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/19\/%e5%b1%9e%e6%80%a7%ef%bc%88property%ef%bc%89-%e5%8f%8d%e5%b0%84%ef%bc%88reflection%ef%bc%89-%e7%9a%84%e5%ae%9e%e6%88%98%e6%a1%88%e4%be%8b\/","title":{"rendered":"\u5c5e\u6027\uff08Property\uff09+ \u53cd\u5c04\uff08Reflection\uff09 \u7684\u5b9e\u6218\u6848\u4f8b"},"content":{"rendered":"\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u57fa\u4e8e <strong>\u5c5e\u6027\uff08Property\uff09+ \u53cd\u5c04\uff08Reflection\uff09<\/strong> \u7684\u5b9e\u6218\u573a\u666f\u793a\u4f8b\uff0c\u6211\u4eec\u5c06\u6784\u5efa\u4e00\u4e2a\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 <strong>\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u6a21\u578b\u6821\u9a8c\u6846\u67b6<\/strong>\uff1a<br>\u901a\u8fc7\u5c5e\u6027\u4e0a\u7684\u81ea\u5b9a\u4e49\u7279\u6027\uff08Attribute\uff09\uff0c\u7ed3\u5408\u53cd\u5c04\u81ea\u52a8\u626b\u63cf\u5e76\u9a8c\u8bc1\u5b57\u6bb5\u662f\u5426\u7b26\u5408\u89c4\u5219\uff08\u5982\u975e\u7a7a\u3001\u957f\u5ea6\u3001\u8303\u56f4\u7b49\uff09\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u529f\u80fd\u76ee\u6807<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u5c5e\u6027\u5b9a\u4e49\u5b9e\u4f53\u7c7b\u5b57\u6bb5\u3002<\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u9a8c\u8bc1 Attribute\uff1a\u5982 <code>[Required]<\/code>\u3001<code>[StringLength]<\/code>\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7\u53cd\u5c04\u81ea\u52a8\u6821\u9a8c\u8fd9\u4e9b\u5c5e\u6027\u3002<\/li>\n\n\n\n<li>\u8f93\u51fa\u9a8c\u8bc1\u7ed3\u679c\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\">\u4e00\u3001\u76ee\u5f55\u7ed3\u6784\uff08\u7c7b\u5212\u5206\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ValidationDemo\/\n\u2502\n\u251c\u2500\u2500 Attributes\/\n\u2502   \u251c\u2500\u2500 RequiredAttribute.cs\n\u2502   \u2514\u2500\u2500 StringLengthAttribute.cs\n\u2502\n\u251c\u2500\u2500 Validators\/\n\u2502   \u2514\u2500\u2500 ObjectValidator.cs\n\u2502\n\u251c\u2500\u2500 Models\/\n\u2502   \u2514\u2500\u2500 User.cs\n\u2502\n\u2514\u2500\u2500 Program.cs<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u4ee3\u7801\u5b9e\u73b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 1. \u81ea\u5b9a\u4e49\u9a8c\u8bc1\u7279\u6027<\/h3>\n\n\n\n<p>\ud83d\udd39 <code>RequiredAttribute.cs<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;AttributeUsage(AttributeTargets.Property)]\npublic class RequiredAttribute : Attribute\n{\n    public string ErrorMessage { get; set; }\n\n    public RequiredAttribute(string errorMessage = \"\u8be5\u5b57\u6bb5\u662f\u5fc5\u586b\u9879\")\n    {\n        ErrorMessage = errorMessage;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <code>StringLengthAttribute.cs<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;AttributeUsage(AttributeTargets.Property)]\npublic class StringLengthAttribute : Attribute\n{\n    public int MaxLength { get; set; }\n    public int MinLength { get; set; }\n    public string ErrorMessage { get; set; }\n\n    public StringLengthAttribute(int minLength = 0, int maxLength = int.MaxValue, string errorMessage = \"\u957f\u5ea6\u4e0d\u7b26\u5408\u8981\u6c42\")\n    {\n        MinLength = minLength;\n        MaxLength = maxLength;\n        ErrorMessage = errorMessage;\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 2. \u5b9a\u4e49\u5b9e\u4f53\u6a21\u578b<\/h3>\n\n\n\n<p>\ud83d\udd39 <code>User.cs<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using ValidationDemo.Attributes;\n\npublic class User\n{\n    &#91;Required(\"\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a\")]\n    &#91;StringLength(3, 20, \"\u7528\u6237\u540d\u957f\u5ea6\u5e94\u57283-20\u4e4b\u95f4\")]\n    public string Username { get; set; }\n\n    &#91;StringLength(6, 100, \"\u5bc6\u7801\u5fc5\u987b\u57286-100\u5b57\u7b26\u4e4b\u95f4\")]\n    public string Password { get; set; }\n\n    &#91;Required(\"\u90ae\u7bb1\u662f\u5fc5\u586b\u9879\")]\n    public string Email { get; set; }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 3. \u6838\u5fc3\u9a8c\u8bc1\u5668<\/h3>\n\n\n\n<p>\ud83d\udd39 <code>ObjectValidator.cs<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Reflection;\nusing ValidationDemo.Attributes;\n\npublic class ObjectValidator\n{\n    public static List&lt;string&gt; Validate(object obj)\n    {\n        var errors = new List&lt;string&gt;();\n\n        if (obj == null) throw new ArgumentNullException(nameof(obj));\n\n        Type type = obj.GetType();\n        PropertyInfo&#91;] props = type.GetProperties();\n\n        foreach (var prop in props)\n        {\n            var value = prop.GetValue(obj);\n\n            \/\/ Required \u68c0\u67e5\n            var requiredAttr = prop.GetCustomAttribute&lt;RequiredAttribute&gt;();\n            if (requiredAttr != null &amp;&amp; (value == null || string.IsNullOrWhiteSpace(value.ToString())))\n            {\n                errors.Add($\"{prop.Name}: {requiredAttr.ErrorMessage}\");\n            }\n\n            \/\/ StringLength \u68c0\u67e5\n            var stringLengthAttr = prop.GetCustomAttribute&lt;StringLengthAttribute&gt;();\n            if (stringLengthAttr != null &amp;&amp; value is string strVal)\n            {\n                if (strVal.Length &lt; stringLengthAttr.MinLength || strVal.Length &gt; stringLengthAttr.MaxLength)\n                {\n                    errors.Add($\"{prop.Name}: {stringLengthAttr.ErrorMessage}\");\n                }\n            }\n        }\n\n        return errors;\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 4. \u6d4b\u8bd5\u5165\u53e3<\/h3>\n\n\n\n<p>\ud83d\udd39 <code>Program.cs<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using ValidationDemo.Models;\nusing ValidationDemo.Validators;\n\nclass Program\n{\n    static void Main()\n    {\n        var user = new User\n        {\n            Username = \"Al\",\n            Password = \"123\", \/\/ \u592a\u77ed\n            Email = \"\" \/\/ \u5fc5\u586b\u4e3a\u7a7a\n        };\n\n        var results = ObjectValidator.Validate(user);\n\n        if (results.Any())\n        {\n            Console.WriteLine(\"\u9a8c\u8bc1\u5931\u8d25\uff1a\");\n            foreach (var error in results)\n                Console.WriteLine($\" - {error}\");\n        }\n        else\n        {\n            Console.WriteLine(\"\u9a8c\u8bc1\u901a\u8fc7\uff01\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u8f93\u51fa\u793a\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\u9a8c\u8bc1\u5931\u8d25\uff1a\n - Username: \u7528\u6237\u540d\u957f\u5ea6\u5e94\u57283-20\u4e4b\u95f4\n - Password: \u5bc6\u7801\u5fc5\u987b\u57286-100\u5b57\u7b26\u4e4b\u95f4\n - Email: \u90ae\u7bb1\u662f\u5fc5\u586b\u9879<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6280\u672f\u70b9\u8bf4\u660e<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6280\u672f<\/th><th>\u5e94\u7528<\/th><\/tr><\/thead><tbody><tr><td>\u5c5e\u6027\uff08Property\uff09<\/td><td>\u5c01\u88c5\u6570\u636e\u5b57\u6bb5<\/td><\/tr><tr><td>\u7279\u6027\uff08Attribute\uff09<\/td><td>\u63cf\u8ff0\u5c5e\u6027\u7684\u6821\u9a8c\u89c4\u5219<\/td><\/tr><tr><td>\u53cd\u5c04\uff08Reflection\uff09<\/td><td>\u83b7\u53d6\u5c5e\u6027\u4fe1\u606f\u548c\u7279\u6027\u3001\u8bfb\u53d6\u503c<\/td><\/tr><tr><td>\u5b9e\u4f53\u6a21\u578b\u6821\u9a8c<\/td><td>\u5c01\u88c5\u6210\u53ef\u590d\u7528\u7684\u9a8c\u8bc1\u5668<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6743\u5a01\u94fe\u63a5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udcd8 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/properties\">\u5fae\u8f6f\u5b98\u65b9 &#8211; C# \u5c5e\u6027<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/concepts\/reflection\">\u5fae\u8f6f\u5b98\u65b9 &#8211; \u53cd\u5c04\u4ecb\u7ecd<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/attributes\/writing-custom-attributes\">\u5fae\u8f6f\u5b98\u65b9 &#8211; Attribute \u57fa\u7840<\/a><\/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\">\u62d3\u5c55\u65b9\u5411\u63a8\u8350<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2705 \u652f\u6301\u66f4\u591a\u7279\u6027\u5982\uff1a<code>RangeAttribute<\/code>\u3001<code>RegexAttribute<\/code><\/li>\n\n\n\n<li>\u2705 \u5c06\u9a8c\u8bc1\u5668\u5c01\u88c5\u4e3a\u4e2d\u95f4\u4ef6\uff0c\u96c6\u6210\u81f3 ASP.NET Core \u4e2d\uff08\u8868\u5355\u6821\u9a8c\uff09<\/li>\n\n\n\n<li>\u2705 \u5c5e\u6027\u7f13\u5b58 + \u8868\u8fbe\u5f0f\u6811\u4ee5\u63d0\u5347\u6027\u80fd\uff08\u907f\u514d\u9891\u7e41\u53cd\u5c04\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u57fa\u4e8e \u5c5e\u6027\uff08Property\uff09+ \u53cd\u5c04\uff08Reflection\uff09 \u7684\u5b9e\u6218\u573a\u666f\u793a\u4f8b\uff0c\u6211\u4eec\u5c06\u6784\u5efa\u4e00\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3566,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3565","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\/3565","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=3565"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3565\/revisions"}],"predecessor-version":[{"id":3567,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3565\/revisions\/3567"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3566"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}