整理过后的代码
通过这个类可以将webservice提供的某个命名空间下的全部的类的实例
并可以通过invokemethod方法调用某个类的实例的方法
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Web; 5using System.Xml; 6using System.Web.Services.Description; 7using System.CodeDom; 8using System.CodeDom.Compiler; 9 10namespace Ecommerce.Web.AppCode 11{ 12 public class DynamicService 13 { 14 private static string _url = string.Empty; 15 16 public static string Url 17 { 18 get { return _url; } 19 set { _url = value; } 20 } 21 22 private static string _nameSpace = string.Empty; 23 24 /// <summary> 25 /// Namespace of the classes 26 /// </summary> 27 public static string NameSpace 28 { 29 get { return _nameSpace; } 30 set { _nameSpace = value; } 31 } 32 33 private static IList<string> _classNames = null; 34 35 /// <summary> 36 /// All classes's names 37 /// </summary> 38 public static IList<string> ClassNames 39 { 40 get { return _classNames; } 41 set 42 { 43 IList<string> nameList = value; 44 45 if (nameList == null || nameList.Count <= 0) 46 throw new Exception("No class name is in the list, please check!"); 47 48 _classNames = nameList; 49 } 50 } 51 52 private static IDictionary<string, object> classList = null; 53 54 /// <summary> 55 /// Get class's instances to use in webservice 56 /// </summary> 57 public static IDictionary<string, object> ClassList 58 { 59 get 60 { 61 IDictionary<string, Object> classNameObjList = new Dictionary<string, Object>(); 62 63 foreach (var item in _classNames) 64 { 65 object instance = InvokeWebservice(item); 66 classNameObjList.Add(item, instance); 67 } 68 69 return classNameObjList; 70 } 71 } 72 73 /// <summary> 74 /// 根据指定的信息,调用远程WebService方法 75 /// </summary> 76 77 /// <param name="classname">欲调用的WebService的类名(不包括命名空间前缀)</param> 78 /// <remarks> 79 /// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。 80 /// 异常信息可能会发生在两个地方: 81 /// 1、动态构造WebService的时候,CompileAssembly失败。 82 /// 2、WebService本身执行失败。 83 /// </remarks> 84 /// <example> 85 /// <code> 86 /// object obj = InvokeWebservice("http://localhost/GSP_WorkflowWebservice/common.asmx","Genersoft.Platform.Service.Workflow","Common","GetToolType",new object[]{"1"}); 87 /// </code> 88 /// </example> 89 public static object InvokeWebservice(string classname) 90 { 91 try 92 { 93 System.Net.WebClient wc = new System.Net.WebClient(); 94 System.IO.Stream stream = wc.OpenRead(_url + "?WSDL"); 95 System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream); 96 System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter(); 97 sdi.AddServiceDescription(sd, "", ""); 98 System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(_nameSpace); 99 System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();100 ccu.Namespaces.Add(cn);101 sdi.Import(cn, ccu);102103 Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();104 System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();105106 System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.CompilerParameters();107 cplist.GenerateExecutable = false;108 cplist.GenerateInMemory = true;109 cplist.ReferencedAssemblies.Add("System.dll");110 cplist.ReferencedAssemblies.Add("System.XML.dll");111 cplist.ReferencedAssemblies.Add("System.Web.Services.dll");112 cplist.ReferencedAssemblies.Add("System.Data.dll");113114 System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);115 if (true == cr.Errors.HasErrors)116 { 117 System.Text.StringBuilder sb = new System.Text.StringBuilder();118 foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)119 { 120 sb.Append(ce.ToString());121 sb.Append(System.Environment.NewLine);122 }123 throw new Exception(sb.ToString());124 }125 System.Reflection.Assembly assembly = cr.CompiledAssembly;126 Type t = assembly.GetType(_nameSpace + "." + classname, true, true);127 object obj = Activator.CreateInstance(t);128129 return obj;130 }131 catch (Exception ex)132 { 133 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));134 }135 }136137 /// <summary>138 /// Invoke the method in this class by specifying the method name139 /// </summary>140 /// <param name="classInstance"></param>141 /// <param name="functionName"></param>142 /// <param name="args"></param>143 /// <returns></returns>144 public static object InvokeMethod(object classInstance, string functionName,object[] args)145 { 146 System.Reflection.MethodInfo mi = classInstance.GetType().GetMethod(functionName);147 return mi.Invoke(classInstance, args);148 }149 }150}151 欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2009/01/06/1370525.html