整理过后的代码
通过这个类可以将webservice提供的某个命名空间下的全部的类的实例
并可以通过invokemethod方法调用某个类的实例的方法
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Web;
5
using System.Xml;
6
using System.Web.Services.Description;
7
using System.CodeDom;
8
using System.CodeDom.Compiler;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
namespace Ecommerce.Web.AppCode
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12
public class DynamicService
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
private static string _url = string.Empty;
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
public static string Url
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _url; }
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _url = value; }
20
}
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
private static string _nameSpace = string.Empty;
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
25
/// Namespace of the classes
26
/// </summary>
27
public static string NameSpace
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _nameSpace; }
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _nameSpace = value; }
31
}
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
private static IList<string> _classNames = null;
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
36
/// All classes's names
37
/// </summary>
38
public static IList<string> ClassNames
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _classNames; }
41
set
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
IList<string> nameList = value;
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
if (nameList == null || nameList.Count <= 0)
46
throw new Exception("No class name is in the list, please check!");
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
_classNames = nameList;
49
}
50
}
51![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
private static IDictionary<string, object> classList = null;
53![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
55
/// Get class's instances to use in webservice
56
/// </summary>
57
public static IDictionary<string, object> ClassList
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59
get
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
IDictionary<string, Object> classNameObjList = new Dictionary<string, Object>();
62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
foreach (var item in _classNames)
64![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
65
object instance = InvokeWebservice(item);
66
classNameObjList.Add(item, instance);
67
}
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
return classNameObjList;
70
}
71
}
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
91
try
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
102![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
104
System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();
105![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
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");
113![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
114
System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
115
if (true == cr.Errors.HasErrors)
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
117
System.Text.StringBuilder sb = new System.Text.StringBuilder();
118
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
128![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
return obj;
130
}
131
catch (Exception ex)
132![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
133
throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
134
}
135
}
136![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
137![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
138
/// Invoke the method in this class by specifying the method name
139
/// </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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
146
System.Reflection.MethodInfo mi = classInstance.GetType().GetMethod(functionName);
147
return mi.Invoke(classInstance, args);
148
}
149
}
150
}
151![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122
做人要厚道,转载请注明出处!