C#对接webservice 接口自动生成

   public bool AutoCode_WebServices(string wsUrl, string[] imPorts, string ClassName, string outputFilePath)
        {
            HttpParamsHelp httpParams = new HttpParamsHelp();
            //ws地址
            httpParams.Uri = wsUrl;
            httpParams.Method = "GET";
            httpParams.KeepAlive = true;
            httpParams.Accept_Charset = "UTF-8";
            //方法数量
            int MethodCount;
            if (httpParams.GetHttpResponse())
            {
                //ws页面数据
                var text = httpParams.ResponseStr.ToString().Replace("\r\n", "").Replace("\r", "").Replace("\n", "").Replace("\t", "");
                //正则匹配方法节点集
                Regex reg = new Regex("<xs:schema.*?</xs:schema");
                Match ma = reg.Match(text);
                string FunContent = ma.Value.ToString();
                if (FunContent.Equals(""))
                {
                    //未匹配到数据
                    return false;
                }
                //创建代码模板
                CodeCompileUnit unit = new CodeCompileUnit();
                CodeNamespace sampleNamespace = new CodeNamespace("song_AutoCode");
                foreach (string import in imPorts)
                {
                    sampleNamespace.Imports.Add(new CodeNamespaceImport(import));
                }
                CodeTypeDeclaration _class = new CodeTypeDeclaration(ClassName);
                //定义生成接口
                _class.IsInterface = true;

                //正则匹配方法名及其参数
                reg = new Regex("<xs:complexType name=\"(.*?)\">(.*?)complexType");
                MatchCollection mags = reg.Matches(FunContent);
                MethodCount = 0;
                foreach (Match mc in mags)
                {
                    CodeMemberMethod method = new CodeMemberMethod();
                    //设置方法返回值 此处默认返回都是String
                    method.ReturnType = new CodeTypeReference("System.String");
                    //接口方法名称
                    method.Name = mc.Groups[1].Value;
                    if (method.Name.Contains("Response"))
                    {
                        //返回部分暂跳过
                        continue;
                    }
                    MethodCount++;
                    string inPut = mc.Groups[2].Value;
                    //正则筛选入参及类型
                    reg = new Regex(".*?name=\"(.*?)\".*?type=\"xs:(.*?\")");
                    mags = reg.Matches(inPut);
                    foreach (Match item in mags)
                    {
                        //参数名
                        string name = item.Groups[1].Value;
                        //入参类型
                        string type = item.Groups[2].Value.Replace("\"", "");
                        switch (type)
                        {
                            case "string":
                            case "decimal":
                                {
                                    type = "System." + type;
                                    break;
                                }
                            case "int":
                                type = "System.Int32";
                                break;
                            case "dateTime":
                                type = "DateTime";
                                break;
                        }
                        method.Parameters.Add(new CodeParameterDeclarationExpression(type, name));
                    }
                    _class.Members.Add(method);
                }

                sampleNamespace.Types.Add(_class);
                unit.Namespaces.Add(sampleNamespace);
                CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
                CodeGeneratorOptions options = new CodeGeneratorOptions();
                options.BracingStyle = "C";
                options.BlankLinesBetweenMembers = true;

                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(outputFilePath + ClassName + ".cs"))
                {
                    provider.GenerateCodeFromCompileUnit(unit, sw, options);
                }
                return true;
            }
            return false;

        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值