首页 新闻 会员 周边 捐助

有木有谁给大致给我讲解一下这段代码呀?

0
[待解决问题]
private static object InvokeWebservice(string url, string classname, string methodname, object[] args)
        {
            try
            {
                string @namespace = "System.EnterpriseServerBase.WebService.DynamicWebCalling";
//此处这个namespace是自己定义的吗?
                //获取WSDL
                System.Net.WebClient wc = new System.Net.WebClient();
                System.IO.Stream stream = wc.OpenRead(url + "?WSDL");//WSDL
                System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
                System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
                sdi.AddServiceDescription(sd, "", "");
                System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace);
                //生成客户端代理类代码
                System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
                ccu.Namespaces.Add(cn);
                sdi.Import(cn, ccu);
                Microsoft.CSharp.CSharpCodeProvider icc = new Microsoft.CSharp.CSharpCodeProvider();
                System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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");
                //编译代理类
                System.CodeDom.Compiler.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(@namespace + "." + classname, true, true);
                object obj = Activator.CreateInstance(t);
                System.Reflection.MethodInfo mi = t.GetMethod(methodname);
                return mi.Invoke(obj, args);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
            }
        }
业荒于嬉的主页 业荒于嬉 | 菜鸟二级 | 园豆:202
提问于:2016-12-30 16:12
< >
分享
所有回答(2)
0

//此处这个namespace是自己定义的吗?

没错,你可以随意定义

czd890 | 园豆:14488 (专家六级) | 2016-12-30 16:25

代码大意就是

1.获取wsdl

2.根据wsdl生成代理类

3.动态创建程序集

4.3的程序集+2的代理类。反射调用webservice接口

支持(1) 反对(0) czd890 | 园豆:14488 (专家六级) | 2016-12-30 16:28
0

就是调用WebService。

terry59599s | 园豆:202 (菜鸟二级) | 2016-12-30 17:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册