{"id":3556,"date":"2025-04-19T10:00:22","date_gmt":"2025-04-19T02:00:22","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3556"},"modified":"2025-04-19T10:00:22","modified_gmt":"2025-04-19T02:00:22","slug":"c-%e8%87%aa%e5%ae%9a%e4%b9%89%e9%aa%8c%e8%af%81%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/19\/c-%e8%87%aa%e5%ae%9a%e4%b9%89%e9%aa%8c%e8%af%81%e6%a1%86%e6%9e%b6\/","title":{"rendered":"C# \u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6846\u67b6"},"content":{"rendered":"\n<p>\u57fa\u4e8e\u4e0a\u7ae0\u8282\uff0c\u8bbe\u8ba1\u4e00\u4e2a\u5b8c\u6574\u7684 <strong>C# \u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6846\u67b6<\/strong> \u5b9e\u6218\u9879\u76ee\uff0c\u4fbf\u4e8e\u5feb\u901f\u638c\u63e1\uff0c\u5177\u6709\u5982\u4e0b\u7279\u70b9\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 \u6846\u67b6\u76ee\u6807<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 Attribute\uff08\u7279\u6027\uff09\u5b9a\u4e49\u9a8c\u8bc1\u89c4\u5219<\/li>\n\n\n\n<li>\u4f7f\u7528 Reflection\uff08\u53cd\u5c04\uff09\u5b9e\u73b0\u9a8c\u8bc1\u903b\u8f91<\/li>\n\n\n\n<li>\u652f\u6301\u81ea\u5b9a\u4e49\u6269\u5c55\u9a8c\u8bc1\u5668<\/li>\n\n\n\n<li>\u652f\u6301\u9a8c\u8bc1\u9519\u8bef\u63d0\u793a\u4fe1\u606f<\/li>\n\n\n\n<li>\u652f\u6301\u9a8c\u8bc1\u5206\u7ec4\uff08Group\uff09\u673a\u5236\uff08\u53ef\u9009\uff09<\/li>\n\n\n\n<li>\u53ef\u96c6\u6210\u5230 ASP.NET Core\u3001WPF\u3001WinForm \u7b49\u9879\u76ee\u4e2d<\/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\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6846\u67b6\u6574\u4f53\u7ed3\u6784<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ValidationFramework\/\n\u251c\u2500\u2500 Attributes\/\n\u2502   \u251c\u2500\u2500 RequiredAttribute.cs\n\u2502   \u251c\u2500\u2500 MaxLengthAttribute.cs\n\u2502   \u2514\u2500\u2500 CustomValidatorAttribute.cs\n\u251c\u2500\u2500 Interfaces\/\n\u2502   \u2514\u2500\u2500 IValidator.cs\n\u251c\u2500\u2500 Validators\/\n\u2502   \u2514\u2500\u2500 ValidationEngine.cs\n\u251c\u2500\u2500 Models\/\n\u2502   \u2514\u2500\u2500 ValidationResult.cs\n\u251c\u2500\u2500 ExampleApp\/\n\u2502   \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\u6846\u67b6\u6838\u5fc3\u5b9e\u73b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 Attribute \u5b9a\u4e49\uff08Attributes\/RequiredAttribute.cs\uff09<\/h3>\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; } = \"\u5b57\u6bb5\u4e0d\u80fd\u4e3a\u7a7a\";\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udc49 \u793a\u4f8b\uff1aMaxLengthAttribute.cs<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;AttributeUsage(AttributeTargets.Property)]\npublic class MaxLengthAttribute : Attribute\n{\n    public int Length { get; }\n    public string ErrorMessage { get; set; }\n\n    public MaxLengthAttribute(int length)\n    {\n        Length = length;\n        ErrorMessage = $\"\u5b57\u6bb5\u957f\u5ea6\u4e0d\u80fd\u8d85\u8fc7 {length}\";\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\">2\ufe0f\u20e3 ValidationResult \u5b9e\u4f53\u7c7b\uff08Models\/ValidationResult.cs\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ValidationResult\n{\n    public bool IsValid =&gt; !Errors.Any();\n    public List&lt;string&gt; Errors { get; set; } = new();\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\">3\ufe0f\u20e3 \u9a8c\u8bc1\u6838\u5fc3\u7c7b\uff08Validators\/ValidationEngine.cs\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public static class ValidationEngine\n{\n    public static ValidationResult Validate(object obj)\n    {\n        var result = new ValidationResult();\n        var props = obj.GetType().GetProperties();\n\n        foreach (var prop in props)\n        {\n            var value = prop.GetValue(obj);\n\n            foreach (var attr in prop.GetCustomAttributes())\n            {\n                switch (attr)\n                {\n                    case RequiredAttribute req:\n                        if (value == null || string.IsNullOrWhiteSpace(value.ToString()))\n                            result.Errors.Add(req.ErrorMessage);\n                        break;\n\n                    case MaxLengthAttribute maxLen:\n                        if (value != null &amp;&amp; value.ToString().Length &gt; maxLen.Length)\n                            result.Errors.Add(maxLen.ErrorMessage);\n                        break;\n                }\n            }\n        }\n\n        return result;\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\">\u4e09\u3001\u4f7f\u7528\u793a\u4f8b\uff08ExampleApp\/Program.cs\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class UserDto\n{\n    &#91;Required(ErrorMessage = \"\u7528\u6237\u540d\u662f\u5fc5\u586b\u9879\")]\n    &#91;MaxLength(10, ErrorMessage = \"\u7528\u6237\u540d\u957f\u5ea6\u4e0d\u80fd\u8d85\u8fc710\")]\n    public string Username { get; set; }\n\n    &#91;MaxLength(5)]\n    public string Nickname { get; set; }\n}\n\nclass Program\n{\n    static void Main(string&#91;] args)\n    {\n        var user = new UserDto\n        {\n            Username = \"\",     \/\/ \u9519\u8bef\uff1aRequired\n            Nickname = \"\u592a\u957f\u7684\u6635\u79f0\" \/\/ \u9519\u8bef\uff1aMaxLength\n        };\n\n        var result = ValidationEngine.Validate(user);\n\n        if (!result.IsValid)\n        {\n            Console.WriteLine(\"\u9a8c\u8bc1\u5931\u8d25:\");\n            foreach (var error in result.Errors)\n                Console.WriteLine($\" - {error}\");\n        }\n        else\n        {\n            Console.WriteLine(\"\u9a8c\u8bc1\u6210\u529f\");\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\">\u56db\u3001\u6269\u5c55\u529f\u80fd\u8bbe\u8ba1<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 \u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668\u63a5\u53e3\uff08Interfaces\/IValidator.cs\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface IValidator\n{\n    bool IsValid(object value);\n    string ErrorMessage { get; }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 \u81ea\u5b9a\u4e49\u9a8c\u8bc1\u7279\u6027\uff08Attributes\/CustomValidatorAttribute.cs\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;AttributeUsage(AttributeTargets.Property)]\npublic class CustomValidatorAttribute : Attribute\n{\n    public Type ValidatorType { get; }\n\n    public CustomValidatorAttribute(Type validatorType)\n    {\n        ValidatorType = validatorType;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 \u5b9e\u73b0\u4e00\u4e2a\u793a\u4f8b\u9a8c\u8bc1\u5668<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class EmailValidator : IValidator\n{\n    public string ErrorMessage =&gt; \"\u90ae\u7bb1\u683c\u5f0f\u4e0d\u6b63\u786e\";\n\n    public bool IsValid(object value)\n    {\n        var str = value as string;\n        return !string.IsNullOrWhiteSpace(str) &amp;&amp; str.Contains(\"@\");\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.4 \u6269\u5c55 ValidationEngine \u652f\u6301\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u5668<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>case CustomValidatorAttribute custom:\n    var validator = (IValidator)Activator.CreateInstance(custom.ValidatorType);\n    if (!validator.IsValid(value))\n        result.Errors.Add(validator.ErrorMessage);\n    break;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u53ef\u9009\u8fdb\u9636\u7279\u6027\uff08\u53ef\u540e\u7eed\u52a0\u5165\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>\u5206\u7ec4\u9a8c\u8bc1<\/td><td>\u53ef\u901a\u8fc7 <code>Group<\/code> \u5c5e\u6027\u63a7\u5236\u9a8c\u8bc1\u9636\u6bb5<\/td><\/tr><tr><td>\u6761\u4ef6\u9a8c\u8bc1<\/td><td>\u901a\u8fc7\u8868\u8fbe\u5f0f\u6811\u63a7\u5236\u67d0\u4e9b\u5b57\u6bb5\u662f\u5426\u89e6\u53d1\u9a8c\u8bc1<\/td><\/tr><tr><td>\u5c5e\u6027\u7ea7\u8054<\/td><td>\u5d4c\u5957\u5bf9\u8c61\u9012\u5f52\u9a8c\u8bc1\uff08\u7c7b\u4f3c FluentValidation\uff09<\/td><\/tr><tr><td>\u652f\u6301\u591a\u8bed\u8a00<\/td><td>\u63d0\u53d6\u9519\u8bef\u63d0\u793a\u5230\u8d44\u6e90\u6587\u4ef6<\/td><\/tr><tr><td>\u4e0e ASP.NET Core \u96c6\u6210<\/td><td>\u5b9e\u73b0 <code>IModelValidator<\/code> \u63a5\u53e3<\/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\">\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udd17 <a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/csharp\/programming-guide\/concepts\/attributes\/\">Attribute (C#)<\/a><\/li>\n\n\n\n<li>\ud83d\udd17 <a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/standard\/reflection\/\">Reflection in .NET<\/a><\/li>\n\n\n\n<li>\ud83d\udd17 <a href=\"https:\/\/fluentvalidation.net\/\">FluentValidation \u793a\u4f8b\uff08\u7c7b\u6bd4\uff09<\/a><\/li>\n\n\n\n<li>\ud83d\udd17 <a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.activator.createinstance\">C# Activator \u52a8\u6001\u521b\u5efa\u5b9e\u4f8b<\/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\">\u603b\u7ed3<\/h2>\n\n\n\n<p>\u8fd9\u4e2a\u9a8c\u8bc1\u6846\u67b6\u5b8c\u6574\u4f53\u73b0\u4e86\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C# \u4e2d Attribute \u7684\u9ad8\u7ea7\u7528\u6cd5<\/li>\n\n\n\n<li>\u53cd\u5c04\u52a8\u6001\u83b7\u53d6\u7c7b\u4fe1\u606f\u3001\u6267\u884c\u903b\u8f91<\/li>\n\n\n\n<li>\u652f\u6301\u81ea\u5b9a\u4e49\u6269\u5c55\u548c\u63d2\u4ef6\u5f0f\u9a8c\u8bc1\u5668\u8bbe\u8ba1<\/li>\n\n\n\n<li>\u53ef\u7075\u6d3b\u5d4c\u5165\u5230\u4efb\u610f\u4e1a\u52a1\u9879\u76ee\u4e2d<\/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>\u57fa\u4e8e\u4e0a\u7ae0\u8282\uff0c\u8bbe\u8ba1\u4e00\u4e2a\u5b8c\u6574\u7684 C# \u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6846\u67b6 \u5b9e\u6218\u9879\u76ee\uff0c\u4fbf\u4e8e\u5feb\u901f\u638c\u63e1\uff0c\u5177\u6709\u5982\u4e0b\u7279\u70b9\uff1a \u2705 \u6846\u67b6\u76ee\u6807 \u4e00\u3001\u81ea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3557,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3556","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\/3556","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=3556"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3556\/revisions"}],"predecessor-version":[{"id":3558,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3556\/revisions\/3558"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3557"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}