无意义的异常调用方法 [英] nonsense argument exception calling method

查看:252
本文介绍了无意义的异常调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个ArgumentException对象类型无法转换为目标类型,但对我来说并不理想。

I'm receiving an ArgumentException "Object type cannot be converted to target type" but it doesn't make very sense to me.

我打电话的方法有以下签名:

the method I'm calling has the following signature:

public void Scan(IProgressStatus monitor, string registryPath, string startupDir, string addinsDir, string databaseDir, string scanFolder, string[] filesToIgnore)

我试图通过监视器而不是 remMonitor 但仍然抛出异常。
所有参数都有值,除了 scanFolder ,它为null(但是传递string.Empty仍然抛出异常)和 filesToIgnore 是一个零长度的数组。

I tried to pass monitor instead of remMonitor but the exception is still thrown. All the arguments have values except scanFolder that is null (but passing string.Empty still throw exception) and filesToIgnore is a zero-length array.

我不知道为什么抛出异常。

I can't figure out why the exception is thrown.

不知道是否有帮助,但进程是64位。如果我从32位进程调用相同的方法,则不会抛出异常,并且运行正常。

Don't know if it helps but the process is 64 bit. If I call the same method from a 32 bit process no exception are thrown and it runs well.

如果我传递null的remMonitor进入该方法。

If I pass null instead of remMonitor it enters the method.

更深入地调试我发现了一些奇怪的东西。我试图打开框来取消选择参数:

Debugging more deeply I found something strange. I've tried to box-unbox the parameter:

rsd.Scan((object)remMonitor, registry.RegistryPath, registry.StartupDirectory, registry.DefaultAddinsFolder, registry.AddinCachePath, scanFolder, filesToIgnore);

public void Scan(object monitor, string registryPath, string startupDir, string addinsDir, string databaseDir, string scanFolder, string[] filesToIgnore)
{
    monitor = (IProgressStatus)monitor;

结果是:

?(IProgressStatus)monitor
Cannot cast 'monitor' (which has an actual type of 'System.Runtime.Remoting.Proxies.__TransparentProxy') to 'Mono.Addins.IProgressStatus'

看起来这个显示器实际上是一个不兼容的类型,现在的问题是为什么?

It looks like that monitor has actually an incompatible type, now the question is why?

确定我已经明白,这是一个DllHell加载上下文问题,我打开了Visual Studio中的所有例外,并说Mono .Addins在LoadFrom上下文中加载。但是如果我写了指令 Assembly.Load(Mono.Addins); 抛出相同的警告(从LoadFrom上载加载)。一些提示?

Ok I've managed to understand that it's a DllHell load context problem, I've turn on all the Exceptions inside Visual Studio and it says that Mono.Addins is load in a LoadFrom context. But if I write the instruction Assembly.Load("Mono.Addins"); the same warning (loaded from LoadFrom context) is thrown. Some hints?

推荐答案

最后我设法解决了我的问题:

Finally I've managed to solve my problem:

如EDIT3所写,这是一个加载上下文问题,这个博客很好地解释了每个方法的加载行为。

As written in EDIT3 it was a load context problem, this blog explains quite well the loading behavior for each method.

因此这个提示我已经手动加载程序集:

Accordingly to this hint I've manually loaded the assembly using:

        System.Reflection.AssemblyName an = new System.Reflection.AssemblyName();
        an.Name = "Mono.Addins";
        an.Version = new System.Version(1, 0, 0);
        System.Reflection.Assembly.Load(an);

然后我放置了正确的程序集 Mono.Addins.dll AutoCAD.exe 路径(即我的应用程序的 ApplicationBase )。原来我以为$ code ApplicationBase 是调用程序集的相同路径,但是没有,我发现我使用的应用程序基础路径 System。 AppDomain.CurrentDomain.BaseDirectory;

Then I've placed the right assembly Mono.Addins.dll inside the AutoCAD.exe path (that is the ApplicationBase for my application). Originally I thought that the ApplicationBase was the same path of the calling assembly but was not, FYI to discover the Application base path I've used System.AppDomain.CurrentDomain.BaseDirectory;

这篇关于无意义的异常调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆