• 动态调用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[0].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 = "201920182017";
    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);
  • 相关阅读:
    [导入]读写文件之日志文件
    [导入]datagrid控件中添加checkbox控件列
    [导入]多态与 new [C#]
    [导入]datagrid控件中添加checkbox控件列
    [导入]多态与 new [C#]
    [导入]读写文件之日志文件
    [导入]多态与 new [C#]
    ABAPJCO
    VB创建类模块DLL文件
    ABAP串口通信道闸设备
  • 原文地址:https://www.cnblogs.com/330774495qq/p/10131894.html
Copyright © 2020-2023  润新知