using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.IO;
// 命名空间包含提供用于处理配置数据的编程模型的类型。
using
System.CodeDom;
*你可以使用该模型“发明”一个自己的.net语言,用你的语言编写程序,再翻译成codeDom,
* 最后编译成可以执行的.net应用程序。
*归根结底在System.CodeDom这个命名空间里你new来new去只能是得到一些Object而已,
* 而Object是什么呢,就是内存里的一点点数据
*/
using
System.CodeDom.Compiler;
* 后一个命名空间在于表现。构造就是搭个架子,把里面的各个部分聚合聚合,连接连接,
* 这个一点点奥秘都没有,所有也不去深究了
*/
using
System.Web.Services;
using
System.Web.Services.Description;
/* 命名空间由使得您可以通过使用 Web 服务描述语言 (WSDL) 来公开描述 XML Web services 的类组成。
* 此命名空间中的每个类都与 WSDL 规范中的某个特定元素相对应,
* 并且类的层次结构与有效的 WSDL 文档的 XML 结构相对应。
* 有关 WSDL 的更多信息,请参见位于 W3C 网站 (http://www.w3.org/TR/wsdl/) 的规范。
*/
using
Microsoft.CSharp;
namespace
InvokeWebService
{
public
static
class
WebServiceHelper
{
/// <summary>
/// 动态调用WebService
/// </summary>
/// <returns>object</returns>
public
static
object
InvokeWebService(
string
url,
string
methodname,
object
[] args)
{
return
InvokeWebService(url,
null
,methodname,args);
}
/// <summary>
/// 动态调用WebService
/// </summary>
/// <returns>object</returns>
public
static
object
InvokeWebService(
string
url,
string
classname,
object
[] args)
{
string
@
namespace
=
"ServiceBase.WebService.DynamicWebLoad"
;
if
(classname ==
null
|| classname ==
""
)
{
classname = WebServiceHelper.GetClassName(url);
}
//(一):获取服务描述语言(WSDL)
//提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法
WebClient wc =
new
WebClient();
Stream stream = wc.OpenRead(url +
"?WSDL"
);
* OpenRea 资源以 Stream 的形式返回数据,对应与OpenWrite()
* OpenReadAsync 在不阻止调用线程的情况下,从资源返回数据
* DownloadData从资源下载数据并返回 Byte 数组。
* DownloadDataAsync在不阻止调用线程的情况下,从资源下载数据并返回 Byte 数组。
* DownloadFile从资源将数据下载到本地文件。
* DownloadString从资源下载 String 并返回 String。
* DownloadStringAsync在不阻止调用线程的情况下,从资源下载 String
*/
ServiceDescription sd = ServiceDescription.Read(stream);
//使用 ServiceDescriptionImporter 类可以方便地将 WSDL 说明中包含的信息导入到
//System.CodeDom.CodeCompileUnit 对象
ServiceDescriptionImporter sdi =
new
ServiceDescriptionImporter();
sdi.AddServiceDescription(sd,
""
,
""
);
CodeNamespace cn =
new
CodeNamespace(@
namespace
);
/*CodeCompileUnit 包含以下几个集合:可以存储包含
* CodeDOM 源代码图形的 CodeNamespace 对象的集合、项目引用的程序集的集合,
* 以及项目程序集的属性集合。
*/
CodeCompileUnit ccu =
new
CodeCompileUnit();
// Add the new namespace to the compile unit.
ccu.Namespaces.Add(cn);
// Add the new namespace import for the System namespace
sdi.Import(cn,ccu);
CSharpCodeProvider csc =
new
CSharpCodeProvider();
*/
ICodeCompiler icc = csc.CreateCompiler();
//(三):设定编译器的参数
cplist.GenerateExecutable =
false
;
cplist.GenerateInMemory =
true
;
cplist.ReferencedAssemblies.Add(
"System.dll"
);
cplist.ReferencedAssemblies.Add(
"System.XML.dll"
);
cplist.ReferencedAssemblies.Add(
"System.Web.Services.dll"
);
cplist.ReferencedAssemblies.Add(
"System.Data.dll"
);
//(四):编译代理类
CompilerResults cr = icc.CompileAssemblyFromDom(cplist,ccu);
if
(
true
== cr.Errors.HasErrors)
{
System.Text.StringBuilder sb =
new
StringBuilder();
foreach
(CompilerError ce
in
cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw
new
Exception(sb.ToString());
}
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(@
namespace
+
"."
+ classname,
true
,
true
);
object
obj = Activator.CreateInstance(t);
return
mi.Invoke(obj,args);
}
private
static
string
GetClassName(
string
url)
{
string
[] parts = url.Split(
'/'
);
string
[] pps = parts[parts.Length - 1].Split(
'.'
);
return
pps[0];
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。