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

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

问题描述

我收到一个 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)

我试图通过 monitor 而不是 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.

根据此提示,我使用:

        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应用).最初我认为 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天全站免登陆