{"id":243369,"date":"2022-06-03T09:27:46","date_gmt":"2022-06-03T01:27:46","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=243369"},"modified":"2022-05-23T14:29:45","modified_gmt":"2022-05-23T06:29:45","slug":"about-c-reflection","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/about-c-reflection.html","title":{"rendered":"\u4e86\u89e3\u4e0bC# \u53cd\u5c04\uff08Reflection\uff09"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u53cd\u5c04\u6307\u7a0b\u5e8f\u53ef\u4ee5\u8bbf\u95ee\u3001\u68c0\u6d4b\u548c\u4fee\u6539\u5b83\u672c\u8eab\u72b6\u6001\u6216\u884c\u4e3a\u7684\u4e00\u79cd\u80fd\u529b\u3002\u7a0b\u5e8f\u96c6\u5305\u542b\u6a21\u5757\uff0c\u800c\u6a21\u5757\u5305\u542b\u7c7b\u578b\uff0c\u7c7b\u578b\u53c8\u5305\u542b\u6210\u5458\u3002\u53cd\u5c04\u5219\u63d0\u4f9b\u4e86\u5c01\u88c5\u7a0b\u5e8f\u96c6\u3001\u6a21\u5757\u548c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

C# \u53cd\u5c04\uff08Reflection\uff09<\/strong><\/div>\n

\u53ef\u4ee5\u4f7f\u7528\u53cd\u5c04\u52a8\u6001\u5730\u521b\u5efa\u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u5c06\u7c7b\u578b\u7ed1\u5b9a\u5230\u73b0\u6709\u5bf9\u8c61\uff0c\u6216\u4ece\u73b0\u6709\u5bf9\u8c61\u4e2d\u83b7\u53d6\u7c7b\u578b\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u8c03\u7528\u7c7b\u578b\u7684\u65b9\u6cd5\u6216\u8bbf\u95ee\u5176\u5b57\u6bb5\u548c\u5c5e\u6027\u3002<\/p>\n

\u4f18\u7f3a\u70b9<\/strong><\/span><\/div>\n

\u4f18\u70b9\uff1a<\/p>\n

    \n
  1. \u53cd\u5c04\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u6269\u5c55\u6027\u3002<\/li>\n
  2. \u964d\u4f4e\u8026\u5408\u6027\uff0c\u63d0\u9ad8\u81ea\u9002\u5e94\u80fd\u529b\u3002<\/li>\n
  3. \u5b83\u5141\u8bb8\u7a0b\u5e8f\u521b\u5efa\u548c\u63a7\u5236\u4efb\u4f55\u7c7b\u7684\u5bf9\u8c61\uff0c\u65e0\u9700\u63d0\u524d\u786c\u7f16\u7801\u76ee\u6807\u7c7b\u3002<\/li>\n<\/ol>\n

    \u7f3a\u70b9\uff1a<\/p>\n

      \n
    1. \u6027\u80fd\u95ee\u9898\uff1a\u4f7f\u7528\u53cd\u5c04\u57fa\u672c\u4e0a\u662f\u4e00\u79cd\u89e3\u91ca\u64cd\u4f5c\uff0c\u7528\u4e8e\u5b57\u6bb5\u548c\u65b9\u6cd5\u63a5\u5165\u65f6\u8981\u8fdc\u6162\u4e8e\u76f4\u63a5\u4ee3\u7801\u3002\u56e0\u6b64\u53cd\u5c04\u673a\u5236\u4e3b\u8981\u5e94\u7528\u5728\u5bf9\u7075\u6d3b\u6027\u548c\u62d3\u5c55\u6027\u8981\u6c42\u5f88\u9ad8\u7684\u7cfb\u7edf\u6846\u67b6\u4e0a\uff0c\u666e\u901a\u7a0b\u5e8f\u4e0d\u5efa\u8bae\u4f7f\u7528\u3002<\/li>\n
    2. \u4f7f\u7528\u53cd\u5c04\u4f1a\u6a21\u7cca\u7a0b\u5e8f\u5185\u90e8\u903b\u8f91\uff1b\u7a0b\u5e8f\u5458\u5e0c\u671b\u5728\u6e90\u4ee3\u7801\u4e2d\u770b\u5230\u7a0b\u5e8f\u7684\u903b\u8f91\uff0c\u53cd\u5c04\u5374\u7ed5\u8fc7\u4e86\u6e90\u4ee3\u7801\u7684\u6280\u672f\uff0c\u56e0\u800c\u4f1a\u5e26\u6765\u7ef4\u62a4\u7684\u95ee\u9898\uff0c\u53cd\u5c04\u4ee3\u7801\u6bd4\u76f8\u5e94\u7684\u76f4\u63a5\u4ee3\u7801\u66f4\u590d\u6742\u3002<\/li>\n<\/ol>\n
      \u53cd\u5c04\uff08Reflection\uff09\u7684\u7528\u9014<\/strong><\/div>\n

      \u53cd\u5c04\uff08Reflection\uff09\u6709\u4e0b\u5217\u7528\u9014\uff1a<\/p>\n

        \n
      1. \u5b83\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u67e5\u770b\u7279\u6027\uff08attribute\uff09\u4fe1\u606f\u3002<\/li>\n
      2. \u5b83\u5141\u8bb8\u5ba1\u67e5\u96c6\u5408\u4e2d\u7684\u5404\u79cd\u7c7b\u578b\uff0c\u4ee5\u53ca\u5b9e\u4f8b\u5316\u8fd9\u4e9b\u7c7b\u578b\u3002<\/li>\n
      3. \u5b83\u5141\u8bb8\u5ef6\u8fdf\u7ed1\u5b9a\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\uff08property\uff09\u3002<\/li>\n
      4. \u5b83\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u521b\u5efa\u65b0\u7c7b\u578b\uff0c\u7136\u540e\u4f7f\u7528\u8fd9\u4e9b\u7c7b\u578b\u6267\u884c\u4e00\u4e9b\u4efb\u52a1\u3002<\/li>\n<\/ol>\n
        \u67e5\u770b\u5143\u6570\u636e<\/strong><\/div>\n

        \u6211\u4eec\u5df2\u7ecf\u5728\u4e0a\u9762\u7684\u7ae0\u8282\u4e2d\u63d0\u5230\u8fc7\uff0c\u4f7f\u7528\u53cd\u5c04\uff08Reflection\uff09\u53ef\u4ee5\u67e5\u770b\u7279\u6027\uff08attribute\uff09\u4fe1\u606f\u3002<\/p>\n

        System.Reflection \u7c7b\u7684 MemberInfo \u5bf9\u8c61\u9700\u8981\u88ab\u521d\u59cb\u5316\uff0c\u7528\u4e8e\u53d1\u73b0\u4e0e\u7c7b\u76f8\u5173\u7684\u7279\u6027\uff08attribute\uff09\u3002\u4e3a\u4e86\u505a\u5230\u8fd9\u70b9\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u76ee\u6807\u7c7b\u7684\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5982\u4e0b\uff1a<\/p>\n

        System.Reflection.MemberInfo info = typeof(MyClass);<\/pre>\n

        \u4e0b\u9762\u7684\u7a0b\u5e8f\u6f14\u793a\u4e86\u8fd9\u70b9\uff1a<\/p>\n

        \u5b9e\u4f8b<\/p>\n

        using System;\r\n\r\n[AttributeUsage(AttributeTargets.All)]\r\npublic class HelpAttribute : System.Attribute\r\n{\r\n   public readonly string Url;\r\n\r\n   public string Topic  \/\/ Topic \u662f\u4e00\u4e2a\u547d\u540d\uff08named\uff09\u53c2\u6570\r\n   {\r\n      get\r\n      {\r\n         return topic;\r\n      }\r\n      set\r\n      {\r\n\r\n         topic = value;\r\n      }\r\n   }\r\n\r\n   public HelpAttribute(string url)  \/\/ url \u662f\u4e00\u4e2a\u5b9a\u4f4d\uff08positional\uff09\u53c2\u6570\r\n   {\r\n      this.Url = url;\r\n   }\r\n\r\n   private string topic;\r\n}\r\n[HelpAttribute(\"Information on the class MyClass\")]\r\nclass MyClass\r\n{\r\n}\r\n\r\nnamespace AttributeAppl\r\n{\r\n   class Program\r\n   {\r\n      static void Main(string[] args)\r\n      {\r\n         System.Reflection.MemberInfo info = typeof(MyClass);\r\n         object[] attributes = info.GetCustomAttributes(true);\r\n         for (int i = 0; i < attributes.Length; i++)\r\n         {\r\n            System.Console.WriteLine(attributes[i]);\r\n         }\r\n         Console.ReadKey();\r\n\r\n      }\r\n   }\r\n}<\/pre>\n

        \u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u663e\u793a\u9644\u52a0\u5230\u7c7b MyClass \u4e0a\u7684\u81ea\u5b9a\u4e49\u7279\u6027\uff1a<\/p>\n

        HelpAttribute<\/pre>\n
        \u5b9e\u4f8b<\/strong><\/div>\n

        \u5728\u672c\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u5728\u4e0a\u4e00\u7ae0\u4e2d\u521b\u5efa\u7684 DeBugInfo \u7279\u6027\uff0c\u5e76\u4f7f\u7528\u53cd\u5c04\uff08Reflection\uff09\u6765\u8bfb\u53d6 Rectangle \u7c7b\u4e2d\u7684\u5143\u6570\u636e\u3002<\/p>\n

        \u5b9e\u4f8b<\/p>\n

        using System;\r\nusing System.Reflection;\r\nnamespace BugFixApplication\r\n{\r\n   \/\/ \u4e00\u4e2a\u81ea\u5b9a\u4e49\u7279\u6027 BugFix \u88ab\u8d4b\u7ed9\u7c7b\u53ca\u5176\u6210\u5458\r\n   [AttributeUsage(AttributeTargets.Class |\r\n   AttributeTargets.Constructor |\r\n   AttributeTargets.Field |\r\n   AttributeTargets.Method |\r\n   AttributeTargets.Property,\r\n   AllowMultiple = true)]\r\n\r\n   public 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   }\r\n   [DeBugInfo(45, \"Zara Ali\", \"12\/8\/2012\",\r\n        Message = \"Return type mismatch\")]\r\n   [DeBugInfo(49, \"Nuha Ali\", \"10\/10\/2012\",\r\n        Message = \"Unused variable\")]\r\n   class 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   }\/\/end class Rectangle  \r\n   \r\n   class ExecuteRectangle\r\n   {\r\n      static void Main(string[] args)\r\n      {\r\n         Rectangle r = new Rectangle(4.5, 7.5);\r\n         r.Display();\r\n         Type type = typeof(Rectangle);\r\n         \/\/ \u904d\u5386 Rectangle \u7c7b\u7684\u7279\u6027\r\n         foreach (Object attributes in type.GetCustomAttributes(false))\r\n         {\r\n            DeBugInfo dbi = (DeBugInfo)attributes;\r\n            if (null != dbi)\r\n            {\r\n               Console.WriteLine(\"Bug no: {0}\", dbi.BugNo);\r\n               Console.WriteLine(\"Developer: {0}\", dbi.Developer);\r\n               Console.WriteLine(\"Last Reviewed: {0}\",\r\n                                        dbi.LastReview);\r\n               Console.WriteLine(\"Remarks: {0}\", dbi.Message);\r\n            }\r\n         }\r\n         \r\n         \/\/ \u904d\u5386\u65b9\u6cd5\u7279\u6027\r\n         foreach (MethodInfo m in type.GetMethods())\r\n         {\r\n            foreach (Attribute a in m.GetCustomAttributes(true))\r\n            {\r\n               DeBugInfo dbi = (DeBugInfo)a;\r\n               if (null != dbi)\r\n               {\r\n                  Console.WriteLine(\"Bug no: {0}, for Method: {1}\",\r\n                                                dbi.BugNo, m.Name);\r\n                  Console.WriteLine(\"Developer: {0}\", dbi.Developer);\r\n                  Console.WriteLine(\"Last Reviewed: {0}\",\r\n                                                dbi.LastReview);\r\n                  Console.WriteLine(\"Remarks: {0}\", dbi.Message);\r\n               }\r\n            }\r\n         }\r\n         Console.ReadLine();\r\n      }\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

        Length: 4.5\r\nWidth: 7.5\r\nArea: 33.75\r\nBug No: 49\r\nDeveloper: Nuha Ali\r\nLast Reviewed: 10\/10\/2012\r\nRemarks: Unused variable\r\nBug No: 45\r\nDeveloper: Zara Ali\r\nLast Reviewed: 12\/8\/2012\r\nRemarks: Return type mismatch\r\nBug No: 55, for Method: GetArea\r\nDeveloper: Zara Ali\r\nLast Reviewed: 19\/10\/2012\r\nRemarks: Return type mismatch\r\nBug No: 56, for Method: Display\r\nDeveloper: Zara Ali\r\nLast Reviewed: 19\/10\/2012\r\nRemarks:<\/pre>\n","protected":false},"excerpt":{"rendered":"

        \u53ef\u4ee5\u4f7f\u7528\u53cd\u5c04\u52a8\u6001\u5730\u521b\u5efa\u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u5c06\u7c7b\u578b\u7ed1\u5b9a\u5230\u73b0\u6709\u5bf9\u8c61\uff0c\u6216\u4ece\u73b0\u6709\u5bf9\u8c61\u4e2d\u83b7\u53d6\u7c7b\u578b\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u8c03\u7528\u7c7b\u578b\u7684\u65b9\u6cd5\u6216\u8bbf\u95ee […]<\/p>\n","protected":false},"author":321,"featured_media":243371,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-243369","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\/243369","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=243369"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243369\/revisions"}],"predecessor-version":[{"id":243374,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243369\/revisions\/243374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/243371"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=243369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=243369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=243369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}