{"id":208537,"date":"2021-02-13T08:35:43","date_gmt":"2021-02-13T00:35:43","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=208537"},"modified":"2021-01-29T10:39:30","modified_gmt":"2021-01-29T02:39:30","slug":"using-reflection-in-c","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/using-reflection-in-c.html","title":{"rendered":"\u6765\u770b\u770b\u5982\u4f55\u5728 C# \u4e2d\u4f7f\u7528\u53cd\u5c04"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nC# \u4e2d\u7684 \u53cd\u5c04 \u5e38\u7528\u4e8e\u5728\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u83b7\u53d6 \u7c7b\u578b \u7684\u5143\u6570\u636e\uff0c\u53ef\u83b7\u53d6\u7684\u4fe1\u606f\u5305\u62ec\u5df2\u52a0\u8f7d\u5230\u8fdb\u7a0b\u4e2d\u7684 \u7a0b\u5e8f\u96c6 \u548c \u7c7b\u578b \u4fe1\u606f\uff0c\u5b83\u548c C++ \u4e2d\u7684 RTTI(Runtime Type Information) \u7684\u4f5c\u7528\u662f\u5dee\u4e0d\u591a\u7684\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

C# \u4e2d\u7684 \u53cd\u5c04 \u5e38\u7528\u4e8e\u5728\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u83b7\u53d6 \u7c7b\u578b \u7684\u5143\u6570\u636e\uff0c\u53ef\u83b7\u53d6\u7684\u4fe1\u606f\u5305\u62ec\u5df2\u52a0\u8f7d\u5230\u8fdb\u7a0b\u4e2d\u7684 \u7a0b\u5e8f\u96c6 \u548c \u7c7b\u578b \u4fe1\u606f\uff0c\u5b83\u548c C++ \u4e2d\u7684 RTTI(Runtime Type Information) \u7684\u4f5c\u7528\u662f\u5dee\u4e0d\u591a\u7684\u3002<\/p>\n

\u4e3a\u4e86\u80fd\u591f\u4f7f\u7528\u53cd\u5c04\uff0c\u9700\u8981\u5728\u9879\u76ee\u4e2d\u5f15\u7528 System.Reflection \u547d\u540d\u7a7a\u95f4\uff0c\u5728\u4f7f\u7528\u53cd\u5c04\u7684\u5f00\u59cb\uff0c\u4f60\u4f1a\u83b7\u53d6\u4e00\u4e2a Type \u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4ece\u8fd9\u4e2a\u5bf9\u8c61\u4e0a\u8fdb\u4e00\u6b65\u83b7\u53d6 \u7a0b\u5e8f\u96c6\uff0c\u7c7b\u578b\uff0c\u6a21\u5757 \u7b49\u4fe1\u606f\uff0c\u53ef\u4ee5\u901a\u8fc7 \u53cd\u5c04 \u52a8\u6001\u7684\u751f\u6210\u67d0\u4e2a\u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u751a\u81f3\u8fd8\u80fd\u52a8\u6001\u8c03\u7528\u8fd9\u4e2a\u7c7b\u578b\u4e0a\u7684\u65b9\u6cd5\u3002<\/p>\n

\u5728 System.Reflection \u547d\u540d\u7a7a\u95f4\u4e0b\uff0c\u5b9a\u4e49\u4e86\u5982\u4e0b\u51e0\u5927\u6838\u5fc3\u7c7b\u578b\u3002<\/p>\n

Assembly\r\nModule\r\nEnum\r\nMethodInfo\r\nConstructorInfo\r\nMemberInfo\r\nParameterInfo\r\nType\r\nFieldInfo\r\nEventInfo\r\nPropertyInfo\r\n<\/pre>\n

\u73b0\u5728\u6211\u4eec\u4e00\u8d77\u7814\u7a76\u4e00\u4e0b\u600e\u4e48\u4f7f\u7528\uff0c\u8003\u8651\u4e0b\u9762\u5b9a\u4e49\u7684 Customer \u7c7b\u3002<\/p>\n

public class Customer \r\n    { \r\n        public int Id { get; set; } \r\n \r\n        public string FirstName { get; set; } \r\n \r\n        public string LastName { get; set; } \r\n \r\n        public string Address { get; set; } \r\n    } \r\n<\/pre>\n

\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7 \u53cd\u5c04 \u6765\u83b7\u53d6 Customer \u7684\u7c7b\u540d\u4ee5\u53ca Customer \u7684\u6240\u5c5e\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n

class Program \r\n   { \r\n       static void Main(string[] args) \r\n       { \r\n           Type type = typeof(Customer); \r\n \r\n           Console.WriteLine(\"Class: \" + type.Name); \r\n           Console.WriteLine(\"Namespace: \" + type.Namespace); \r\n       } \r\n   } \r\n<\/pre>\n

\"\"
\n\u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5982\u4f55\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6 Customer \u4e0b\u7684\u6240\u6709\u5c5e\u6027\uff0c\u5e76\u4e14\u5c06\u5c5e\u6027\u540d\u5b57\u5168\u90e8\u5c55\u793a\u5728\u63a7\u5236\u53f0\u4e0a\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n

static void Main(string[] args) \r\n        { \r\n            Type type = typeof(Customer); \r\n \r\n            PropertyInfo[] propertyInfo = type.GetProperties(); \r\n \r\n            Console.WriteLine(\"The list of properties of the Customer class are:--\"); \r\n \r\n            foreach (PropertyInfo pInfo in propertyInfo) \r\n            { \r\n                Console.WriteLine(pInfo.Name); \r\n            } \r\n        } \r\n<\/pre>\n

\"\"
\n\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0ctypeof(Customer).GetProperties() \u9ed8\u8ba4\u53ea\u80fd\u83b7\u53d6 \u6807\u8bb0\u4e3a public \u7684\u5c5e\u6027\u96c6\u5408\uff0c\u5bf9\u5e94\u7740 Customer \u7c7b\u4e0b\u7684\u56db\u4e2a\u516c\u5f00\u5c5e\u6027\u3002<\/p>\n

\u63a5\u4e0b\u6765\u518d\u6765\u770b\u770b\u5982\u4f55\u901a\u8fc7 \u53cd\u5c04 \u83b7\u53d6\u7c7b\u578b\u4e0b\u7684 \u6784\u9020\u51fd\u6570 \u548c \u516c\u5171\u65b9\u6cd5 \u7684\u5143\u6570\u636e\u4fe1\u606f\uff0c\u8fd9\u91cc\u8fd8\u662f\u7ee7\u7eed\u4f7f\u7528 Customer \u7c7b\uff0c\u5728\u7c7b\u4e2d\u65b0\u589e\u4e00\u4e2a \u6784\u9020\u51fd\u6570 \u548c\u4e00\u4e2a Validate \u65b9\u6cd5\uff0c\u6b64\u65b9\u6cd5\u7528\u4e8e\u6821\u9a8c\u5165\u53c2\u7684\u5408\u6cd5\u6027\uff0c\u4e0b\u9762\u5c31\u662f\u4fee\u6539\u540e\u7684 Customer \u7c7b\u3002<\/p>\n

public class Customer \r\n    { \r\n        public int Id { get; set; } \r\n \r\n        public string FirstName { get; set; } \r\n \r\n        public string LastName { get; set; } \r\n \r\n        public string Address { get; set; } \r\n \r\n        public Customer() { } \r\n \r\n        public bool Validate(Customer customerObj) \r\n        { \r\n            \/\/Code to validate the customer object \r\n            return true; \r\n        } \r\n    } \r\n<\/pre>\n

\u7136\u540e\u518d\u6765\u770b\u770b\u901a\u8fc7 \u53cd\u5c04 \u6765\u83b7\u53d6 Customer \u4e0b\u6240\u6709\u5b9a\u4e49\u7684\u6784\u9020\u51fd\u6570\uff0c\u4e0d\u8fc7\u8fd9\u91cc\u53ea\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u56e0\u6b64\u53ea\u80fd\u5217\u51fa\u4e00\u4e2a\u3002<\/p>\n

class Program \r\n    { \r\n        static void Main(string[] args) \r\n        { \r\n            Type type = typeof(Customer); \r\n \r\n            ConstructorInfo[] constructorInfo = type.GetConstructors(); \r\n \r\n            Console.WriteLine(\"The Customer class contains the following Constructors:--\"); \r\n \r\n            foreach (ConstructorInfo c in constructorInfo) \r\n            { \r\n                Console.WriteLine(c); \r\n            } \r\n        } \r\n    } \r\n<\/pre>\n

\"\"
\n\u540c\u6837\u4e5f\u8981\u6ce8\u610f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b GetConstructors() \u65b9\u6cd5\u53ea\u80fd\u83b7\u53d6 Customer \u7684\u6240\u6709\u6807\u8bb0\u4e3a public \u7684\u6784\u9020\u51fd\u6570\u3002<\/p>\n

\u63a5\u4e0b\u6765\u770b\u770b\u5982\u4f55\u5c55\u793a Customer \u4e2d\u7684\u6240\u6709 public \u65b9\u6cd5\uff0c\u56e0\u4e3a\u8be5\u7c7b\u4e2d\u53ea\u5b9a\u4e49\u4e86\u4e00\u4e2a public \u65b9\u6cd5\uff0c\u6240\u4ee5\u63a7\u5236\u53f0\u4e0a\u4e5f\u5e94\u8be5\u53ea\u4f1a\u5c55\u793a\u4e00\u4e2a\uff0c\u5982\u4e0b\u4ee3\u7801\u4ec5\u4f9b\u53c2\u8003\u3002<\/p>\n

static void Main(string[] args) \r\n       { \r\n           Type type = typeof(Customer); \r\n \r\n           MethodInfo[] methodInfo = type.GetMethods(); \r\n \r\n           Console.WriteLine(\"The methods of the Customer class are:--\"); \r\n \r\n           foreach (MethodInfo temp in methodInfo) \r\n           { \r\n               Console.WriteLine(temp.Name); \r\n           } \r\n \r\n           Console.Read(); \r\n       } \r\n<\/pre>\n

\"\"
\n\u662f\u4e0d\u662f\u5f88\u60ca\u8bb6\uff0c\u521a\u624d\u8fd8\u8bf4\u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5c45\u7136\u591a\u4e86\u597d\u51e0\u4e2a\uff0c\u8981\u77e5\u9053\u591a\u7684\u90a3\u51e0\u4e2a\u65b9\u6cd5\uff0c\u6765\u81ea\u4e8e\u4e24\u65b9\u9762\u3002<\/p>\n

\u4ece object \u7c7b\u578b\u7ee7\u627f\u4e0b\u6765\u7684\u516c\u5171\u65b9\u6cd5
\n\"\"<\/p>\n

\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u7684\u5c5e\u6027\u65b9\u6cd5
\n\"\"<\/p>\n

\u5982\u679c\u65b9\u6cd5\u4e0a\u9762\u6807\u8bb0\u4e86 Attribute, \u8fd8\u53ef\u4ee5\u901a\u8fc7 GetCustomAttributes \u65b9\u6cd5\u6765\u83b7\u53d6\uff0c\u53c2\u8003\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

static void Main(string[] args) \r\n        { \r\n            foreach (MethodInfo temp in methodInfo) \r\n            { \r\n                foreach (Attribute attribute in temp.GetCustomAttributes(true)) \r\n                { \r\n                    \/\/Write your usual code here \r\n                } \r\n            } \r\n        } \r\n<\/pre>\n

\u76f8\u4fe1\u5728\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u5728 \u9886\u57df\u5b9e\u4f53 \u4e0a\u4f7f\u7528\u5404\u79cd Attribute \u7279\u6027\uff0c\u8fd9\u65f6\u5019\u5c31\u53ef\u4ee5\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\u53cd\u5c04\u63d0\u53d6 \u9886\u57df\u5b9e\u4f53 \u4e2d\u7684\u65b9\u6cd5\u4e0a\u7684Attribute\u4fe1\u606f\uff0c\u4ece\u800c\u6839\u636e\u63d0\u53d6\u5230\u7684 Attribute \u6267\u884c\u4f60\u7684\u5177\u4f53\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

C# \u4e2d\u7684 \u53cd\u5c04 \u5e38\u7528\u4e8e\u5728\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u83b7\u53d6 \u7c7b\u578b \u7684\u5143\u6570\u636e\uff0c\u53ef\u83b7\u53d6\u7684\u4fe1\u606f\u5305\u62ec\u5df2\u52a0\u8f7d\u5230\u8fdb\u7a0b\u4e2d\u7684 \u7a0b\u5e8f\u96c6 \u548c \u7c7b\u578b […]<\/p>\n","protected":false},"author":1898,"featured_media":210737,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[633],"class_list":["post-208537","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-c--"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208537","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\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=208537"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208537\/revisions"}],"predecessor-version":[{"id":210738,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208537\/revisions\/210738"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/210737"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=208537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=208537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=208537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}