动态编译/WebService动态调用
动态编译
CSharpCodeProvider 类
提供对 C# 代码生成器和代码编译器的实例的访问
方法 CompileAssemblyFromSource
从包含源代码的字符串的指定数组,使用指定的编译器设置编译程序集。
public virtual CompilerResults CompileAssemblyFromSource(
CompilerParameters options,
params string[] sources
)
CompilerParameters options,
params string[] sources
)
CompilerParameters类
表示 ICodeCompiler 接口的设置和选项的 CompilerParameters 对象。
如果正在编译可执行程序,则必须将 GenerateExecutable 属性设置为 true。当 GenerateExecutable 设置为 false 时,编译器将生成一个类库。 默认为 false。
如果要从 CodeDOM 图形编译可执行文件,则必须在该图形中定义 CodeEntryPointMethod。
如果有多个代码入口点,则可以通过如下方法指示定义要使用的入口点的类:将 MainClass 属性设置为该类的名称。
可以在 OutputAssembly 属性中指定输出程序集的文件名。 如果不指定,则使用默认的输出文件名。
要在生成的程序集中包括调试信息,请将 IncludeDebugInformation 属性设置为 true。 如果项目引用了任何程序集,则必须将引用的程序集名称指定为 StringCollection 中的项,设置到在调用编译时使用的 CompilerParameters 的 ReferencedAssemblies 属性中。
通过将 GenerateInMemory 属性设置为 true,可以编译写入内存而不是磁盘中的程序集。
当在内存中生成程序集时,代码可从 CompilerResults 的 CompiledAssembly 属性中获取生成的程序集的引用。
如果程序集被写入到磁盘中,则可以从 CompilerResults 的 PathToAssembly 属性获取到生成的程序集的路径。
CompilerResults 类
表示从编译器返回的编译结果。
此类包含了以下有关由 ICodeCompiler 接口实现所产生的编译结果的信息:
- CompiledAssembly 属性指示编译的程序集。
- Evidence 属性指示程序集的安全证据。
- PathToAssembly 属性指示编译的程序集的路径(如果不是只在内存中生成编译的程序集)。
- Errors 属性指示任何编译器错误和警告。
- Output 属性包含编译器输出消息。
- NativeCompilerReturnValue 属性指示编译器返回的结果代码值。
- TempFiles 属性指示编译和链接过程中生成的临时文件
WebService动态调用
WebClient 类
提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法。
Credentials
获取或设置发送到主机并用于对请求进行身份验证的网络凭据
ServiceDescription 类
提供一种方法,以创建和格式化用于描述 XML Web services 的有效的 Web 服务描述语言 (WSDL) 文档文件,该文件是完整的,具有适当的命名空间、元素和属性。无法继承此类。
ServiceDescriptionImporter 类
公开一种为 XML Web services 生成客户端代理类的方法。使用 ServiceDescriptionImporter 类可以方便地将 WSDL 说明中包含的信息导入到 System.CodeDom .CodeCompileUnit 对象。 通过调整 Style 参数的值,可以指示 ServiceDescriptionImporter 实例生成客户端代理类(通过透明调用该类可提供 Web 服务的功能)或生成抽象类(该类封装 Web 服务的功能而不实现该功能)。然后可直接调用或以选择的语言导出生成的 CodeCompileUnit 对象中的代码。
WebClient 类
提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法。
Credentials
获取或设置发送到主机并用于对请求进行身份验证的网络凭据
ServiceDescription 类
提供一种方法,以创建和格式化用于描述 XML Web services 的有效的 Web 服务描述语言 (WSDL) 文档文件,该文件是完整的,具有适当的命名空间、元素和属性。无法继承此类。
ServiceDescriptionImporter 类
公开一种为 XML Web services 生成客户端代理类的方法。使用 ServiceDescriptionImporter 类可以方便地将 WSDL 说明中包含的信息导入到 System.CodeDom .CodeCompileUnit 对象。 通过调整 Style 参数的值,可以指示 ServiceDescriptionImporter 实例生成客户端代理类(通过透明调用该类可提供 Web 服务的功能)或生成抽象类(该类封装 Web 服务的功能而不实现该功能)。然后可直接调用或以选择的语言导出生成的 CodeCompileUnit 对象中的代码。
public static object InvokeWebService( WebServiceModel model)
{
if ((model.ClassName == null ) || (model.ClassName == ""))
{
model.ClassName = WebServiceHelper .GetWsClassName(model.Url);
}
try
{
//获?取??WSDL
WebClient wc = new WebClient();
if (model.IsNeedcrede)
{
NetworkCredential credential = new NetworkCredential(model.WsAccount, model.WsPassword);
wc.Credentials = credential;
}
Stream stream = wc.OpenRead(model.Url + "?WSDL");
ServiceDescription sd = ServiceDescription .Read(stream);
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "" , "" );
CodeNamespace cn = new CodeNamespace(model.Namespace);
//生成客户端代理代码
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider csc = new CSharpCodeProvider();
ICodeCompiler icc = csc.CreateCompiler();
//设定编译参数
CompilerParameters cplist = new CompilerParameters();
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 System.Text. StringBuilder();
foreach (System.CodeDom.Compiler.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(model.Namespace + "." + model.ClassName, true , true );
object obj = Activator .CreateInstance(t);
System.Reflection. MethodInfo mi = t.GetMethod(model.MethodName);
return mi.Invoke(obj, model.Args);
}
catch (Exception ex)
{
throw new Exception(ex.InnerException.Message, new Exception (ex.InnerException.StackTrace));
}
}

浙公网安备 33010602011771号