+ -
当前位置:首页 → 问答吧 → Invoke、InvokeMember提示“调用的目标发生了异常”

Invoke、InvokeMember提示“调用的目标发生了异常”

时间:2011-12-10

来源:互联网

本人使用软件三层架构,也就是有一个实体类Entity,数据层Data这种架构。由于要给PDA访问,因此建立了一个简易的中间件,中间件根据TCP接收回来的指令,使用反射的方法调用Entity和Data的各个方法。
   
  现在遇到一个问题,Data中的异常无法反馈到中间件上,无论什么类型的错误,中间件总是得到引发“调用的目标发生了异常”的异常。
   
  要知道,Data中的异常是不可避免的,例如数据库超时、安装时数据库配置错误等。唯有的办法是把异常反馈到前台,让用户能够看到是什么样的异常才好。
  请问有何解决之道。

下付测试用的部分源码:
C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReflectionTestDll
{
    public class Class1
    {
        public Class1()
        {
        }

        public string MethodErr2(string pValues, string pValues1)
        {
            throw new Exception("MethodErr2 " + pValues + pValues1);
        }
    }
}



C# code


private void btnMethodErr2_Click(object sender, EventArgs e)
{
    try
    {
        Assembly t = Assembly.Load("ReflectionTestDll");
        Type a = t.GetType("ReflectionTestDll.Class1");

        string[] bb = new string[] { "aaaa", "bbbbb" };
        object obj = Activator.CreateInstance(a, bb);

        MethodInfo mi = a.GetMethod("MethodErr2");
        object[] aa = new string[] { "For Err ", "Method2" };
        string s = (string)mi.Invoke(obj, aa); //带参数方法的调用
    }
    catch (Exception Ex)
    {
        //我希望这里能够获得异常,可是每次都是“调用的目标发生了异常”
    }

    this.txtMSG.Text = mSB.ToString();
}




作者: cnwolfs   发布时间: 2011-12-10

帖子快沉了,帮顶一下。

作者: dalmeeme   发布时间: 2011-12-10

相关阅读 更多

热门下载

更多