{"id":3547,"date":"2025-04-19T09:50:35","date_gmt":"2025-04-19T01:50:35","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3547"},"modified":"2025-04-19T09:50:35","modified_gmt":"2025-04-19T01:50:35","slug":"c-%e7%89%b9%e6%80%a7%ef%bc%88attribute%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/19\/c-%e7%89%b9%e6%80%a7%ef%bc%88attribute%ef%bc%89\/","title":{"rendered":"C# \u7279\u6027\uff08Attribute\uff09"},"content":{"rendered":"\n<p>\u672c\u7ae0\u8282\u56f4\u7ed5\u4ee5\u4e0b\u4e03\u4e2a\u65b9\u9762\u6765\u7cfb\u7edf\u8bb2\u89e3<strong>C# \u7684\u7279\u6027\uff08Attribute\uff09<\/strong>\uff0c\u80fd\u591f\u5168\u9762\u6df1\u5165\u4e86\u89e3 <strong>C# \u7684\u7279\u6027\uff08Attribute\uff09<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4ec0\u4e48\u662f Attribute\uff08\u7279\u6027\uff09\uff1f<\/h2>\n\n\n\n<p>\u5728 C# \u4e2d\uff0c<strong>\u7279\u6027\uff08Attribute\uff09<\/strong> \u662f\u4e00\u79cd\u7528\u4e8e\u5411\u7a0b\u5e8f\u7684\u5143\u7d20\uff08\u5982\u7c7b\u3001\u65b9\u6cd5\u3001\u5c5e\u6027\u3001\u5b57\u6bb5\u7b49\uff09\u6dfb\u52a0\u5143\u6570\u636e\u7684\u673a\u5236\u3002<\/p>\n\n\n\n<p>\u5b83\u4e0d\u4f1a\u76f4\u63a5\u5f71\u54cd\u7a0b\u5e8f\u8fd0\u884c\u903b\u8f91\uff0c\u800c\u662f\u53ef\u4ee5\u901a\u8fc7 <strong>\u53cd\u5c04<\/strong> \u5728\u8fd0\u884c\u65f6\u8bfb\u53d6\uff0c\u7528\u4e8e\u63a7\u5236\u884c\u4e3a\u3001\u914d\u7f6e\u4fe1\u606f\u3001\u5b9e\u73b0\u6846\u67b6\u652f\u6301\u7b49\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b80\u5355\u8bf4\uff1a<strong>Attribute \u662f\u544a\u8bc9\u7a0b\u5e8f\u201c\u8fd9\u662f\u4e00\u4e9b\u989d\u5916\u7684\u4fe1\u606f\u201d\uff0c\u4f9b\u8fd0\u884c\u65f6\u6216\u6846\u67b6\u4f7f\u7528\u3002<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>\ud83d\udcd8 <strong>\u5b98\u65b9\u6587\u6863<\/strong>\uff1a<br>https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/csharp\/programming-guide\/concepts\/attributes\/<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001Attribute \u7684\u4f7f\u7528\u573a\u666f<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u6807\u6ce8\u5e8f\u5217\u5316\u89c4\u5219<\/td><td>\u5982 <code>[JsonProperty(\"name\")]<\/code> \u63a7\u5236\u5e8f\u5217\u5316\u5b57\u6bb5<\/td><\/tr><tr><td>\u63a7\u5236\u53cd\u5c04\u884c\u4e3a<\/td><td>\u63a7\u5236\u662f\u5426\u53c2\u4e0e\u53cd\u5c04\u3001\u6807\u8bc6\u65b9\u6cd5\u7528\u9014<\/td><\/tr><tr><td>\u58f0\u660e\u6027\u7f16\u7a0b<\/td><td>\u6bd4\u5982 <code>[Obsolete]<\/code> \u6807\u8bb0\u65b9\u6cd5\u8fc7\u65f6<\/td><\/tr><tr><td>ASP.NET \u914d\u7f6e<\/td><td>\u5982 <code>[HttpGet]<\/code>\u3001<code>[Route(\"api\/test\")]<\/code><\/td><\/tr><tr><td>\u81ea\u5b9a\u4e49\u9a8c\u8bc1<\/td><td>\u8868\u5355\u6a21\u578b\u9a8c\u8bc1 <code>[Required]<\/code>, <code>[MaxLength]<\/code> \u7b49<\/td><\/tr><tr><td>\u5355\u5143\u6d4b\u8bd5<\/td><td>\u5982 <code>[TestMethod]<\/code>, <code>[Fact]<\/code>, <code>[Theory]<\/code><\/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\">\u4e09\u3001Attribute \u7684\u8bed\u6cd5\u57fa\u7840<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4f7f\u7528\u7cfb\u7edf\u5185\u7f6e Attribute<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Obsolete(\"\u8bf7\u4e0d\u8981\u518d\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\")]\npublic void OldMethod()\n{\n    \/\/ \u65e7\u903b\u8f91\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udd0d <code>ObsoleteAttribute<\/code> \u4f1a\u5728\u7f16\u8bd1\u65f6\u53d1\u51fa\u8b66\u544a\/\u9519\u8bef\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u7279\u6027\u57fa\u672c\u8bed\u6cd5\u7ed3\u6784<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;AttributeName(positional_argument, named_argument=value)]<\/code><\/pre>\n\n\n\n<p>\u547d\u540d\u89c4\u8303\u662f\uff1a\u7279\u6027\u7c7b\u4ee5 <code>Attribute<\/code> \u7ed3\u5c3e\uff0c\u4f46\u4f7f\u7528\u65f6\u53ef\u4ee5\u7701\u7565\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u81ea\u5b9a\u4e49 Attribute<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b9a\u4e49\u7279\u6027\u7c7b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]\npublic class MyNoteAttribute : Attribute\n{\n    public string Note { get; }\n\n    public MyNoteAttribute(string note)\n    {\n        Note = note;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5e94\u7528\u7279\u6027<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;MyNote(\"\u8fd9\u4e2a\u7c7b\u7528\u4e8e\u6f14\u793a\")]\npublic class DemoClass\n{\n    &#91;MyNote(\"\u8c03\u7528\u524d\u8bf7\u9a8c\u8bc1\")]\n    public void DoSomething() { }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528\u53cd\u5c04\u8bfb\u53d6\u7279\u6027<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var type = typeof(DemoClass);\nvar attributes = type.GetCustomAttributes(typeof(MyNoteAttribute), false);\n\nforeach (MyNoteAttribute attr in attributes)\n{\n    Console.WriteLine(attr.Note);\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\">\u4e94\u3001\u5b9e\u6218\u6848\u4f8b\uff1a\u57fa\u4e8e\u7279\u6027 + \u53cd\u5c04\u5b9e\u73b0\u63a5\u53e3\u6743\u9650\u63a7\u5236<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u573a\u666f\uff1a\u6211\u4eec\u4e3a API \u65b9\u6cd5\u6807\u8bb0\u89d2\u8272\u6743\u9650\u7279\u6027\uff0c\u5e76\u5728\u6267\u884c\u524d\u6821\u9a8c\u3002<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u2705 1. \u5b9a\u4e49\u7279\u6027\u7c7b<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;AttributeUsage(AttributeTargets.Method)]\npublic class RoleAttribute : Attribute\n{\n    public string RequiredRole { get; }\n\n    public RoleAttribute(string role)\n    {\n        RequiredRole = role;\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u2705 2. \u5728\u65b9\u6cd5\u4e2d\u6807\u6ce8<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public class UserController\n{\n    &#91;Role(\"Admin\")]\n    public void DeleteUser() { }\n\n    &#91;Role(\"User\")]\n    public void ViewProfile() { }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u2705 3. \u8fd0\u884c\u65f6\u5224\u65ad\u6743\u9650<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public void ExecuteWithRoleCheck(string methodName, string userRole)\n{\n    var method = typeof(UserController).GetMethod(methodName);\n    var attr = method.GetCustomAttribute&lt;RoleAttribute&gt;();\n\n    if (attr != null &amp;&amp; attr.RequiredRole != userRole)\n    {\n        throw new UnauthorizedAccessException(\"\u6743\u9650\u4e0d\u8db3\");\n    }\n\n    method.Invoke(new UserController(), null);\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\">\u516d\u3001\u5e38\u7528\u7cfb\u7edf\u5185\u7f6e Attribute<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>[Obsolete]<\/code><\/td><td>\u6807\u8bb0\u4e0d\u63a8\u8350\u4f7f\u7528\u7684\u65b9\u6cd5<\/td><\/tr><tr><td><code>[Serializable]<\/code><\/td><td>\u6807\u8bb0\u53ef\u4ee5\u5e8f\u5217\u5316\u7684\u7c7b<\/td><\/tr><tr><td><code>[NonSerialized]<\/code><\/td><td>\u6807\u8bb0\u5b57\u6bb5\u4e0d\u53c2\u4e0e\u5e8f\u5217\u5316<\/td><\/tr><tr><td><code>[DllImport]<\/code><\/td><td>\u7528\u4e8e\u8c03\u7528\u975e\u6258\u7ba1 DLL<\/td><\/tr><tr><td><code>[Conditional]<\/code><\/td><td>\u6839\u636e\u6761\u4ef6\u7f16\u8bd1<\/td><\/tr><tr><td><code>[DefaultValue]<\/code><\/td><td>\u8bbe\u7f6e\u9ed8\u8ba4\u503c<\/td><\/tr><tr><td><code>[Description]<\/code><\/td><td>\u6dfb\u52a0\u63cf\u8ff0\u4fe1\u606f\uff0c\u5e38\u7528\u4e8e UI \u6216\u914d\u7f6e<\/td><\/tr><tr><td><code>[Required]<\/code>, <code>[Range]<\/code><\/td><td>ASP.NET \u8868\u5355\u9a8c\u8bc1\u7279\u6027<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udc49 \u8be6\u89c1\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/learn.microsoft.com\/zh-cn\/dotnet\/standard\/attributes\/<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u6700\u4f73\u5b9e\u8df5\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e0d\u8981\u6ee5\u7528\u7279\u6027<\/strong>\uff1a\u4ec5\u5728\u9700\u8981\u8868\u8fbe\u5143\u4fe1\u606f\u6216\u8fd0\u884c\u65f6\u52a8\u6001\u5904\u7406\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong>\u5408\u7406\u4f7f\u7528 AttributeUsage<\/strong> \u63a7\u5236\u7279\u6027\u5e94\u7528\u8303\u56f4\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &#91;AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u7ed3\u5408\u53cd\u5c04\u8c28\u614e\u5904\u7406\u6027\u80fd\u95ee\u9898<\/strong>\uff0c\u5c24\u5176\u662f\u5728\u5faa\u73af\u4f53\u4e2d\u9891\u7e41\u53cd\u5c04\u3002<\/li>\n\n\n\n<li><strong>\u591a\u7528\u4e8e\u58f0\u660e\u6027\u7f16\u7a0b\u548c\u5143\u6570\u636e\u9a71\u52a8\u7684\u6846\u67b6\uff08\u5982 ASP.NET\u3001EF Core\u3001XUnit\uff09<\/strong>\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u76f8\u5173\u6743\u5a01\u53c2\u8003\u94fe\u63a5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/csharp\/programming-guide\/concepts\/attributes\/\">C# Attributes &#8211; Microsoft Docs\uff08\u5b98\u65b9\uff09<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.attribute\">Attribute \u7c7b &#8211; System \u547d\u540d\u7a7a\u95f4<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.attributeusageattribute\">AttributeUsageAttribute \u7c7b\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/tagged\/attributes+c%23\">Stack Overflow &#8211; \u4f7f\u7528 Attribute \u7684\u6700\u4f73\u5b9e\u8df5<\/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>C# \u7684 Attribute \u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684 <strong>\u58f0\u660e\u6027\u5143\u6570\u636e\u7f16\u7a0b\u80fd\u529b<\/strong>\uff0c\u53ef\u4ee5\u8ba9\u4ee3\u7801\u7ed3\u6784\u66f4\u6e05\u6670\u3001\u529f\u80fd\u66f4\u5f3a\u5927\uff0c\u7279\u522b\u9002\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6846\u67b6\u6784\u5efa\uff08\u6bd4\u5982 ASP.NET MVC\uff09<\/strong><\/li>\n\n\n\n<li><strong>\u5e8f\u5217\u5316\u63a7\u5236\uff08\u5982 JSON\u3001XML\uff09<\/strong><\/li>\n\n\n\n<li><strong>\u52a8\u6001\u884c\u4e3a\u9a71\u52a8\uff08\u5982\u6743\u9650\u3001\u65e5\u5fd7\u3001\u8def\u7531\uff09<\/strong><\/li>\n\n\n\n<li><strong>\u4ee3\u7801\u5206\u6790\u5de5\u5177\u4e0e\u6ce8\u89e3<\/strong><\/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>\u672c\u7ae0\u8282\u56f4\u7ed5\u4ee5\u4e0b\u4e03\u4e2a\u65b9\u9762\u6765\u7cfb\u7edf\u8bb2\u89e3C# \u7684\u7279\u6027\uff08Attribute\uff09\uff0c\u80fd\u591f\u5168\u9762\u6df1\u5165\u4e86\u89e3 C# \u7684\u7279\u6027\uff08Attri [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3548,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3547","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\/3547","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=3547"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3547\/revisions"}],"predecessor-version":[{"id":3549,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3547\/revisions\/3549"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3548"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}