Conditional<\/strong><\/span><\/div>\n\u8fd9\u4e2a\u9884\u5b9a\u4e49\u7279\u6027\u6807\u8bb0\u4e86\u4e00\u4e2a\u6761\u4ef6\u65b9\u6cd5\uff0c\u5176\u6267\u884c\u4f9d\u8d56\u4e8e\u6307\u5b9a\u7684\u9884\u5904\u7406\u6807\u8bc6\u7b26\u3002<\/p>\n
\u5b83\u4f1a\u5f15\u8d77\u65b9\u6cd5\u8c03\u7528\u7684\u6761\u4ef6\u7f16\u8bd1\uff0c\u53d6\u51b3\u4e8e\u6307\u5b9a\u7684\u503c\uff0c\u6bd4\u5982 Debug \u6216 Trace\u3002\u4f8b\u5982\uff0c\u5f53\u8c03\u8bd5\u4ee3\u7801\u65f6\u663e\u793a\u53d8\u91cf\u7684\u503c\u3002<\/p>\n
\u89c4\u5b9a\u8be5\u7279\u6027\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n
[Conditional(\r\n conditionalSymbol\r\n)<\/pre>\n]
\n\u4f8b\u5982\uff1a<\/p>\n
[Conditional(\"DEBUG\")]<\/pre>\n\u4e0b\u9762\u7684\u5b9e\u4f8b\u6f14\u793a\u4e86\u8be5\u7279\u6027\uff1a<\/p>\n
\u5b9e\u4f8b<\/p>\n
#define DEBUG\r\nusing System;\r\nusing System.Diagnostics;\r\npublic class Myclass\r\n{\r\n [Conditional(\"DEBUG\")]\r\n public static void Message(string msg)\r\n {\r\n Console.WriteLine(msg);\r\n }\r\n}\r\nclass Test\r\n{\r\n static void function1()\r\n {\r\n Myclass.Message(\"In Function 1.\");\r\n function2();\r\n }\r\n static void function2()\r\n {\r\n Myclass.Message(\"In Function 2.\");\r\n }\r\n public static void Main()\r\n {\r\n Myclass.Message(\"In Main function.\");\r\n function1();\r\n Console.ReadKey();\r\n }\r\n}<\/pre>\n\u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n
In Main function.\r\nIn Function 1.\r\nIn Function 2.<\/pre>\nObsolete<\/strong><\/span><\/div>\n\u8fd9\u4e2a\u9884\u5b9a\u4e49\u7279\u6027\u6807\u8bb0\u4e86\u4e0d\u5e94\u88ab\u4f7f\u7528\u7684\u7a0b\u5e8f\u5b9e\u4f53\u3002\u5b83\u53ef\u4ee5\u8ba9\u60a8\u901a\u77e5\u7f16\u8bd1\u5668\u4e22\u5f03\u67d0\u4e2a\u7279\u5b9a\u7684\u76ee\u6807\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5f53\u4e00\u4e2a\u65b0\u65b9\u6cd5\u88ab\u7528\u5728\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u4f46\u662f\u60a8\u4ecd\u7136\u60f3\u8981\u4fdd\u6301\u7c7b\u4e2d\u7684\u65e7\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u663e\u793a\u4e00\u4e2a\u5e94\u8be5\u4f7f\u7528\u65b0\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u65e7\u65b9\u6cd5\u7684\u6d88\u606f\uff0c\u6765\u628a\u5b83\u6807\u8bb0\u4e3a obsolete\uff08\u8fc7\u65f6\u7684\uff09\u3002<\/p>\n
\u89c4\u5b9a\u8be5\u7279\u6027\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n
[Obsolete(\r\n message\r\n)]\r\n[Obsolete(\r\n message,\r\n iserror\r\n)]<\/pre>\n\u5176\u4e2d\uff1a<\/p>\n
\n- \u53c2\u6570 message\uff0c\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u63cf\u8ff0\u9879\u76ee\u4e3a\u4ec0\u4e48\u8fc7\u65f6\u4ee5\u53ca\u8be5\u66ff\u4ee3\u4f7f\u7528\u4ec0\u4e48\u3002<\/li>\n
- \u53c2\u6570 iserror\uff0c\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5982\u679c\u8be5\u503c\u4e3a true\uff0c\u7f16\u8bd1\u5668\u5e94\u628a\u8be5\u9879\u76ee\u7684\u4f7f\u7528\u5f53\u4f5c\u4e00\u4e2a\u9519\u8bef\u3002\u9ed8\u8ba4\u503c\u662f false\uff08\u7f16\u8bd1\u5668\u751f\u6210\u4e00\u4e2a\u8b66\u544a\uff09\u3002<\/li>\n<\/ol>\n
\u4e0b\u9762\u7684\u5b9e\u4f8b\u6f14\u793a\u4e86\u8be5\u7279\u6027\uff1a<\/p>\n
\u5b9e\u4f8b<\/p>\n
using System;\r\npublic class MyClass\r\n{\r\n [Obsolete(\"Don't use OldMethod, use NewMethod instead\", true)]\r\n static void OldMethod()\r\n {\r\n Console.WriteLine(\"It is the old method\");\r\n }\r\n static void NewMethod()\r\n {\r\n Console.WriteLine(\"It is the new method\");\r\n }\r\n public static void Main()\r\n {\r\n OldMethod();\r\n }\r\n}<\/pre>\n\u5f53\u60a8\u5c1d\u8bd5\u7f16\u8bd1\u8be5\u7a0b\u5e8f\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u7ed9\u51fa\u4e00\u4e2a\u9519\u8bef\u6d88\u606f\u8bf4\u660e\uff1a<\/p>\n
Don't use OldMethod, use NewMethod instead<\/pre>\n\u521b\u5efa\u81ea\u5b9a\u4e49\u7279\u6027\uff08Attribute\uff09<\/strong><\/div>\n.Net \u6846\u67b6\u5141\u8bb8\u521b\u5efa\u81ea\u5b9a\u4e49\u7279\u6027\uff0c\u7528\u4e8e\u5b58\u50a8\u58f0\u660e\u6027\u7684\u4fe1\u606f\uff0c\u4e14\u53ef\u5728\u8fd0\u884c\u65f6\u88ab\u68c0\u7d22\u3002\u8be5\u4fe1\u606f\u6839\u636e\u8bbe\u8ba1\u6807\u51c6\u548c\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\uff0c\u53ef\u4e0e\u4efb\u4f55\u76ee\u6807\u5143\u7d20\u76f8\u5173\u3002<\/p>\n
\u521b\u5efa\u5e76\u4f7f\u7528\u81ea\u5b9a\u4e49\u7279\u6027\u5305\u542b\u56db\u4e2a\u6b65\u9aa4\uff1a<\/p>\n
\n- \u58f0\u660e\u81ea\u5b9a\u4e49\u7279\u6027<\/li>\n
- \u6784\u5efa\u81ea\u5b9a\u4e49\u7279\u6027<\/li>\n
- \u5728\u76ee\u6807\u7a0b\u5e8f\u5143\u7d20\u4e0a\u5e94\u7528\u81ea\u5b9a\u4e49\u7279\u6027<\/li>\n
- \u901a\u8fc7\u53cd\u5c04\u8bbf\u95ee\u7279\u6027<\/li>\n<\/ol>\n
\u6700\u540e\u4e00\u4e2a\u6b65\u9aa4\u5305\u542b\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u6765\u8bfb\u53d6\u5143\u6570\u636e\u4ee5\u4fbf\u67e5\u627e\u5404\u79cd\u7b26\u53f7\u3002\u5143\u6570\u636e\u662f\u7528\u4e8e\u63cf\u8ff0\u5176\u4ed6\u6570\u636e\u7684\u6570\u636e\u548c\u4fe1\u606f\u3002\u8be5\u7a0b\u5e8f\u5e94\u4f7f\u7528\u53cd\u5c04\u6765\u5728\u8fd0\u884c\u65f6\u8bbf\u95ee\u7279\u6027\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u70b9\u3002<\/p>\n
\u58f0\u660e\u81ea\u5b9a\u4e49\u7279\u6027<\/strong><\/span><\/div>\n\u4e00\u4e2a\u65b0\u7684\u81ea\u5b9a\u4e49\u7279\u6027\u5e94\u6d3e\u751f\u81ea System.Attribute \u7c7b\u3002\u4f8b\u5982\uff1a<\/p>\n
\/\/ \u4e00\u4e2a\u81ea\u5b9a\u4e49\u7279\u6027 BugFix \u88ab\u8d4b\u7ed9\u7c7b\u53ca\u5176\u6210\u5458\r\n[AttributeUsage(AttributeTargets.Class |\r\nAttributeTargets.Constructor |\r\nAttributeTargets.Field |\r\nAttributeTargets.Method |\r\nAttributeTargets.Property,\r\nAllowMultiple = true)]\r\n\r\npublic class DeBugInfo : System.Attribute<\/pre>\n\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a DeBugInfo \u7684\u81ea\u5b9a\u4e49\u7279\u6027\u3002<\/p>\n
\u6784\u5efa\u81ea\u5b9a\u4e49\u7279\u6027<\/strong><\/span><\/div>\n\u8ba9\u6211\u4eec\u6784\u5efa\u4e00\u4e2a\u540d\u4e3a DeBugInfo \u7684\u81ea\u5b9a\u4e49\u7279\u6027\uff0c\u8be5\u7279\u6027\u5c06\u5b58\u50a8\u8c03\u8bd5\u7a0b\u5e8f\u83b7\u5f97\u7684\u4fe1\u606f\u3002\u5b83\u5b58\u50a8\u4e0b\u9762\u7684\u4fe1\u606f\uff1a<\/p>\n
\n- bug \u7684\u4ee3\u7801\u7f16\u53f7<\/li>\n
- \u8fa8\u8ba4\u8be5 bug \u7684\u5f00\u53d1\u4eba\u5458\u540d\u5b57<\/li>\n
- \u6700\u540e\u4e00\u6b21\u5ba1\u67e5\u8be5\u4ee3\u7801\u7684\u65e5\u671f<\/li>\n
- \u4e00\u4e2a\u5b58\u50a8\u4e86\u5f00\u53d1\u4eba\u5458\u6807\u8bb0\u7684\u5b57\u7b26\u4e32\u6d88\u606f<\/li>\n<\/ol>\n
\u6211\u4eec\u7684 DeBugInfo \u7c7b\u5c06\u5e26\u6709\u4e09\u4e2a\u7528\u4e8e\u5b58\u50a8\u524d\u4e09\u4e2a\u4fe1\u606f\u7684\u79c1\u6709\u5c5e\u6027\uff08property\uff09\u548c\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u6d88\u606f\u7684\u516c\u6709\u5c5e\u6027\uff08property\uff09\u3002\u6240\u4ee5 bug \u7f16\u53f7\u3001\u5f00\u53d1\u4eba\u5458\u540d\u5b57\u548c\u5ba1\u67e5\u65e5\u671f\u5c06\u662f DeBugInfo \u7c7b\u7684\u5fc5\u9700\u7684\u5b9a\u4f4d\uff08 positional\uff09\u53c2\u6570\uff0c\u6d88\u606f\u5c06\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u547d\u540d\uff08named\uff09\u53c2\u6570\u3002<\/p>\n
\u6bcf\u4e2a\u7279\u6027\u5fc5\u987b\u81f3\u5c11\u6709\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002\u5fc5\u9700\u7684\u5b9a\u4f4d\uff08 positional\uff09\u53c2\u6570\u5e94\u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f20\u9012\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86 DeBugInfo \u7c7b\uff1a<\/p>\n
\u5b9e\u4f8b<\/p>\n
\/\/ \u4e00\u4e2a\u81ea\u5b9a\u4e49\u7279\u6027 BugFix \u88ab\u8d4b\u7ed9\u7c7b\u53ca\u5176\u6210\u5458\r\n[AttributeUsage(AttributeTargets.Class |\r\nAttributeTargets.Constructor |\r\nAttributeTargets.Field |\r\nAttributeTargets.Method |\r\nAttributeTargets.Property,\r\nAllowMultiple = true)]\r\n\r\npublic class DeBugInfo : System.Attribute\r\n{\r\n private int bugNo;\r\n private string developer;\r\n private string lastReview;\r\n public string message;\r\n\r\n public DeBugInfo(int bg, string dev, string d)\r\n {\r\n this.bugNo = bg;\r\n this.developer = dev;\r\n this.lastReview = d;\r\n }\r\n\r\n public int BugNo\r\n {\r\n get\r\n {\r\n return bugNo;\r\n }\r\n }\r\n public string Developer\r\n {\r\n get\r\n {\r\n return developer;\r\n }\r\n }\r\n public string LastReview\r\n {\r\n get\r\n {\r\n return lastReview;\r\n }\r\n }\r\n public string Message\r\n {\r\n get\r\n {\r\n return message;\r\n }\r\n set\r\n {\r\n message = value;\r\n }\r\n }\r\n}<\/pre>\n\u5e94\u7528\u81ea\u5b9a\u4e49\u7279\u6027<\/strong><\/span><\/div>\n\u901a\u8fc7\u628a\u7279\u6027\u653e\u7f6e\u5728\u7d27\u63a5\u7740\u5b83\u7684\u76ee\u6807\u4e4b\u524d\uff0c\u6765\u5e94\u7528\u8be5\u7279\u6027\uff1a<\/p>\n
\u5b9e\u4f8b<\/p>\n
[DeBugInfo(45, \"Zara Ali\", \"12\/8\/2012\", Message = \"Return type mismatch\")]\r\n[DeBugInfo(49, \"Nuha Ali\", \"10\/10\/2012\", Message = \"Unused variable\")]\r\nclass Rectangle\r\n{\r\n \/\/ \u6210\u5458\u53d8\u91cf\r\n protected double length;\r\n protected double width;\r\n public Rectangle(double l, double w)\r\n {\r\n length = l;\r\n width = w;\r\n }\r\n [DeBugInfo(55, \"Zara Ali\", \"19\/10\/2012\",\r\n Message = \"Return type mismatch\")]\r\n public double GetArea()\r\n {\r\n return length * width;\r\n }\r\n [DeBugInfo(56, \"Zara Ali\", \"19\/10\/2012\")]\r\n public void Display()\r\n {\r\n Console.WriteLine(\"Length: {0}\", length);\r\n Console.WriteLine(\"Width: {0}\", width);\r\n Console.WriteLine(\"Area: {0}\", GetArea());\r\n }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"\u7279\u6027\uff08Attribute\uff09\u7528\u4e8e\u6dfb\u52a0\u5143\u6570\u636e\uff0c\u5982\u7f16\u8bd1\u5668\u6307\u4ee4\u548c\u6ce8\u91ca\u3001\u63cf\u8ff0\u3001\u65b9\u6cd5\u3001\u7c7b\u7b49\u5176\u4ed6\u4fe1\u606f\u3002.Net \u6846\u67b6\u63d0\u4f9b\u4e86\u4e24 […]<\/p>\n","protected":false},"author":321,"featured_media":243365,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-243363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=243363"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243363\/revisions"}],"predecessor-version":[{"id":243368,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243363\/revisions\/243368"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/243365"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=243363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=243363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=243363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}