动态调用WebService(传对象返回接受对象)
基础属性
//客户端代理服务命名空间,可以设置成需要的值。
string ns = string.Format("WindowsForms");
private Assembly assembly;
// 服务端程序Assembly
public Assembly Assembly
{
get { return assembly; }
set { assembly = value; }
}
private object agent;
//获取服务端程序集
public object Agent
{
get { return agent; }
set { agent = value; }
} private Type agentType;
//获取程序集类型
public Type AgentType
{
get { return agentType; }
set { agentType = value; }
}
下面是构造函数,自己命名
public 构造函数(string url)
{
//获取WSDL
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url + "?WSDL");
ServiceDescription sd = ServiceDescription.Read(stream);//服务的描述信息都可以通过ServiceDescription获取
string classname = sd.Services[].Name;
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
CodeNamespace cn = new CodeNamespace(ns);
//生成客户端代理类代码
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider csc = new CSharpCodeProvider();
//设定编译参数
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 = csc.CompileAssemblyFromDom(cplist, ccu);
if (cr.Errors.HasErrors == true)
{
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());
}
Assembly = cr.CompiledAssembly;
AgentType = Assembly.GetType(ns + "." + classname, true, true);
Agent = Activator.CreateInstance(agentType);
}
两个函数用于,转对象和对象赋值
/// <summary>
/// 获取存在的对象并赋值
/// </summary>
/// <param name="methodName"></param>
/// <returns></returns>
public object SerializeObj<T>(string propertieName, T t)
{
Type ObjType = Assembly.GetType(ns + "." + propertieName, true, true);
object obj = Activator.CreateInstance(ObjType);
if (obj == null) throw new Exception("no " + propertieName);
var Types = t.GetType();//获得类型
Type tt = obj.GetType();
foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段
{
FieldInfo fi = tt.GetField(sp.Name);
if (fi != null)
{
fi.SetValue(obj, sp.GetValue(t, null));
}
}
return obj;
}
/// <summary>
/// 获取的Service程序集对象并赋值
/// </summary>
public void DeserializeObj<T>(T t, object obj)
{
var Types = t.GetType();//获得类型
Type tt = obj.GetType();
foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段
{
FieldInfo fi = tt.GetField(sp.Name);
if (fi != null)
{
sp.SetValue(t, fi.GetValue(obj), null);
}
}
}
最后Demo调用
//返回对象并把数据赋值到指定对象
WebServicesWsdl WebService = new WebServicesWsdl("WebService.asmx");
Field = "";
Fields = new object[] { Field };
_return = WebService.GetMethod("函数名称", Fields);
Model model = new Model();
WebService.DeserializeObj<Model >(model, _return);
//将对象当参数传过去
WebServicesWsdl WebInterface = new WebServicesWsdl("WebInterface.asmx");
var Field = WebInterface.SerializeObj<Model>("对象名称", model);
object[] Fields = new object[]{ Field };
object _return = WebInterface.GetMethod("函数名称", Fields);
动态调用WebService(传对象返回接受对象)的更多相关文章
- 动态调用WebService(C#) (非常实用)
通常我们在程序中需要调用WebService时,都是通过“添加Web引用”,让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务.这样是使工作简单了,但是却和提供Web服务的URL.方法名 ...
- 动态调用WebService(C#)
通常我们在程序中需要调用WebService时,都是通过“添加Web引用”,让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务.这样是使工作简单了,但是却和提供Web服务的URL.方法名 ...
- 用C#通过反射实现动态调用WebService 告别Web引用(转载)
我们都知道,调用WebService可以在工程中对WebService地址进行WEB引用,但是这确实很不方便.我想能够利用配置文件灵活调用WebService.如何实现呢? 用C#通过反射实现动态调用 ...
- C# .NET 动态调用webservice的三种方式
转载自 百度文库 http://wenku.baidu.com/link?url=Q2q50wohf5W6UX44zqotXFEe_XOMaib4UtI3BigaNwipOHKNETloMF4ax4W ...
- 用C#通过反射实现动态调用WebService 告别Web引用
我们都知道,调用WebService可以在工程中对WebService地址进行WEB引用,但是这确实很不方便.我想能够利用配置文件灵活调用WebService.如何实现呢? 用C#通过反射实现动态调用 ...
- 动态调用Webservice 支持Soapheader身份验证(转)
封装的WebserviceHelp类: using System; using System.CodeDom; using System.CodeDom.Compiler; using System. ...
- [转]Net 下采用GET/POST/SOAP方式动态调用WebService C#实现
本文转自:http://www.cnblogs.com/splendidme/archive/2011/10/05/2199501.html 一直以来,我们都为动态调用WebService方法而烦恼. ...
- .Net 下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) [轉]Redfox
一直以来,我都为动态调用WebService方法而烦恼.在.Net环境下,最常用的方法就是采用代理类来调用WebService,可以通过改变代理类的Url属性来实现动态调用,但当xmlns改变时就会出 ...
- Java动态调用webService,axis2动态调用webService
Java动态调用webService axis2动态调用webService >>>>>>>>>>>>>>>& ...
随机推荐
- CC2640蓝牙芯片开发备记
server ,characteristic UUID ,handle sever ,一个工程里可以有多个服务,按键服务,心率计服务,马达服务. characteristic , 一个服务可以有多个的 ...
- Angular 2/4/5+ 重复点击菜单刷新界面
记一下,网上没找到方法 自己搞了好久 通过跳转到别的界面在跳回来的方式进行实现 //再次点击刷新界面 if (this.router.url == item.ur ...
- golang反射
要点 1.变量 2.反射 3.结构体反射 4.反射总结以及应用场景 一.变量介绍 1.变量的内在机制 A.类型信息,这部分是元信息,是预定义好的 B.值类型,这部分是程序运行过程中,动态改变的 var ...
- 前端测试时,常用SQL
与后台联调页面,经常会验证数据正确性,这时候就需要会点SQL语句,以下是常用的SQL: 单表查询 select * from table 条件查询 select * from table where ...
- Python matplotlib绘图学习笔记
测试环境: Jupyter QtConsole 4.2.1Python 3.6.1 1. 基本画线: 以下得出红蓝绿三色的点 import numpy as npimport matplotlib. ...
- 配置xml报错:URI is not registered ( Setting | Project Settings | Schemas and DTDs )
报红提示:URI is not registered ( Setting | Project Settings | Schemas and DTDs ) 解决方法:打开Schemas and DTDs ...
- JS调用本地设备
JS 允许通过 navigator.mediaDevices.getUserMedia(options) 直接调用本地的设备,比如麦克风,摄像头等.因为该操作涉及到用户隐私,所以调用的时候会弹框请求权 ...
- python从零开始 -- 第2篇之python版本差异
python从零开始 -- 第2篇之python版本差异 第0篇开始,咱们就说选择 python 3.x,一般来说,咱们面临选择的时候总是想了解更多一点,并且版本之间的对比能引申出很多有意思的故事和知 ...
- Python学习之路基础篇--10Python基础,函数进阶
1 命名空间 对于Python 来说命名空间一共有三种 1 内置命名空间 —— Python 解释器 就是Python 解释器一启动就可以使用的名字,储存在内置命名空间中.内置的名字在启动解释器的时候 ...
- es6学习笔记-set和map数据结构
ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. const s = new Set(); [2, 3 ...