{"id":854,"date":"2013-09-10T17:48:44","date_gmt":"2013-09-10T09:48:44","guid":{"rendered":"http:\/\/www.vcoo.cc\/blog\/?p=854"},"modified":"2013-09-10T17:48:44","modified_gmt":"2013-09-10T09:48:44","slug":"csharp-reflection","status":"publish","type":"post","link":"https:\/\/vcoo.cc\/blog\/854\/","title":{"rendered":"C#\u53cd\u5c04 &#8211; \u6839\u636e\u7c7b\u540d\u5b57\u7b26\u4e32\u5b9e\u4f8b\u5316\u7c7b"},"content":{"rendered":"<p>\u5f85\u5b8c\u5584\u2026\u2026<\/p>\n<p>[csharp]using System;<br \/>\nusing System.Collections.Generic;<br \/>\nusing System.Text;<br \/>\nusing System.Reflection;<\/p>\n<p>namespace SearchFromDB<br \/>\n{<br \/>\n    class Reflection<br \/>\n    {<br \/>\n        string ClassName=&quot;&quot;;<br \/>\n        Type clstype;<br \/>\n        public Reflection(string ClassName)<br \/>\n        {<br \/>\n            this.ClassName = ClassName;<br \/>\n        }<br \/>\n        public Reflection()<br \/>\n        {<\/p>\n<p>        }<br \/>\n        \/\/\/ &lt;summary&gt;<br \/>\n        \/\/\/ \u5b9e\u4f8b\u5bf9\u8c61\u65f6\u9700\u8981\u6307\u5b9a\u7c7b\u540d<br \/>\n        \/\/\/ &lt;\/summary&gt;<br \/>\n        public object GetClassInstance(string assembly ,string NameSpace)<br \/>\n        {<br \/>\n            \/\/assembly\u4e3a\u7a0b\u5e8f\u96c6\u540d\u79f0\uff0cNameSpace\u4e3a\u547d\u540d\u7a7a\u95f4<\/p>\n<p>            clstype = Assembly.Load(assembly).GetType(string.Concat(NameSpace, &quot;.&quot;, this.ClassName));<br \/>\n            if (clstype == null)<br \/>\n                return null;<br \/>\n            object obj = (object)Activator.CreateInstance(clstype);<br \/>\n            return obj;<br \/>\n        }<br \/>\n        \/\/\/ &lt;summary&gt;<br \/>\n        \/\/\/ \u5b9e\u4f8b\u5bf9\u8c61\u65f6\u4e0d\u7528\u6307\u5b9a\u7c7b\u540d<br \/>\n        \/\/\/ &lt;\/summary&gt;<br \/>\n        public object GetClassInstance(string assembly,string NameSpace,string classname)<br \/>\n        {<br \/>\n            ClassName = classname;<br \/>\n            clstype = Assembly.Load(assembly).GetType(string.Concat(NameSpace, &quot;.&quot;, classname));<br \/>\n            if (clstype == null)<br \/>\n                return null;<br \/>\n            object obj = (object)Activator.CreateInstance(clstype);<br \/>\n            return obj;<br \/>\n        }<br \/>\n        \/\/\/ &lt;summary&gt;<br \/>\n        \/\/\/ \u6267\u884c\u7c7b\u7684\u9759\u6001\u65b9\u6cd5<br \/>\n        \/\/\/ &lt;\/summary&gt;<br \/>\n        \/\/\/ &lt;param name=&quot;methodname&quot;&gt;<br \/>\n        \/\/\/ \u7c7b\u7684\u65b9\u6cd5\u540d<br \/>\n        \/\/\/ &lt;\/param&gt;<br \/>\n        \/\/\/ &lt;param name=&quot;parameters&quot;&gt;<br \/>\n        \/\/\/ \u65b9\u6cd5\u7684\u53c2\u6570\u7c7b\u578b<br \/>\n        \/\/\/ &lt;\/param&gt;<br \/>\n        \/\/\/ &lt;param name=&quot;methodtype&quot;&gt;<br \/>\n        \/\/\/ \u65b9\u6cd5\u7684\u53c2\u6570<br \/>\n        \/\/\/ &lt;\/param&gt;<\/p>\n<p>        public object GetMethod(string methodname, Type[] methodtype, object[] parameters)<br \/>\n        {<br \/>\n            \/\/ methodtype.SetValue(typeof(string),1);<br \/>\n            System.Reflection.MethodInfo pMethod = clstype.GetMethod(methodname, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public, null, methodtype, null);<br \/>\n            \/\/\u8c03\u7528\u65b9\u6cd5\u7684\u4e00\u4e9b\u6807\u5fd7\u4f4d\uff0c\u8fd9\u91cc\u7684\u542b\u4e49\u662fPublic\u5e76\u4e14\u662f\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u8fd9\u4e5f\u662f\u9ed8\u8ba4\u7684\u503c<br \/>\n            \/\/System.Reflection.BindingFlags flag = BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public;<br \/>\n            object returnValue = pMethod.Invoke(null, parameters);<br \/>\n            \/\/string returnValue = pMethod.Invoke(clsObj, flag, Type.DefaultBinder, parameters,null).ToString();<br \/>\n            return returnValue;<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/csharp]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f85\u5b8c\u5584\u2026\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[117,201,246],"class_list":["post-854","post","type-post","status-publish","format-standard","hentry","category-csharp","tag-c","tag-dotnet","tag-246","cat-13-id"],"_links":{"self":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts\/854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/comments?post=854"}],"version-history":[{"count":0,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts\/854\/revisions"}],"wp:attachment":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/media?parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/categories?post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/tags?post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}