现在的位置: 首页 > 综合 > 正文

调用webService的一个例子(反射、传参)

2013年03月02日 ⁄ 综合 ⁄ 共 1487字 ⁄ 字号 评论关闭

【转】使用反射动态实例化一个类,出现未能加载文件或程序集

http://social.microsoft.com/Forums/zh-CN/295/thread/267f8e34-06be-4baa-a923-ed185271163c

反射方法调用时的一个错误:参数计数不匹配( parameter count mismatch )

http://www.cnblogs.com/binarytree/archive/2010/04/21/1717491.html

 

我的一个反射传参的例子

webService:

       public object DoF(string sFunctionName,string sQueryString)
        {
            try
            {
                string sServerPath = Server.MapPath("");
                //string sTmp = System.AppDomain.CurrentDomain.BaseDirectory;
                Assembly asm = Assembly.LoadFrom(sServerPath + @"\bin\BusinessRule.dll");//加载前面生成的程序集
                
                //未能加载文件或程序集“file:///C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\bin\Debug\BusinessRule.dll”或它的某一个依赖项。系统找不到指定的文件。
                Type t = asm.GetType("BusinessRule.Class1");
                object[] param =null;

                if (!string.IsNullOrEmpty(sQueryString))
                    param = sQueryString.Split(',');

                object o = Activator.CreateInstance(t);
                MethodInfo method = t.GetMethod("F" + sFunctionName);
                param = new object[] { param };
                var result = method.Invoke(o, param);
                return result;
            }
            catch (Exception ex)
            {
                //return ex.Message;                
                return "未实现的函数:" + sFunctionName;
            }
        }

businessRule.Class1:

        public static string F100(string[] _sQueryString)
        {
            if (_sQueryString!=null && _sQueryString.Length>0)
            {
                int sum = 0;
                foreach (string i in _sQueryString)
                {
                    sum += int.Parse(i);
                }
                return sum.ToString();
            }
            else {
                return "";
            }
        }

UI:

        private void button1_Click(object sender, EventArgs e)
        {
            BBB.MyService se = new BBB.MyService();
            var result = se.DoF(txtFunctionName.Text.Trim(),txtQueryString.Text.Trim());
            txtResult.Text = result.ToString();
        }

UI 传参: 

txtFunctionName:100

txtQueryString: 309,33

通过 webservice调用,最终得到结果: 342

抱歉!评论已关闭.