微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐