nonsense argument exception calling method

那年仲夏 提交于 2019-12-11 13:49:27

问题


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)

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.

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.

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

[EDIT2] 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);

and

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

The result is:

?(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?

[EDIT3] 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?


回答1:


Found this over in a Google Group, and it mentions the same exception on the same method:

https://groups.google.com/forum/#!msg/mono-addins/f4SzNPtcRIg/ma9EPEzGdagJ

In SetupDomain.cs:Scan() there is a line that reads: rsd.Scan (remMonitor, registryPath, startupDir, scanFolder, filesToIgnore);

If I step into this method I'm not taken to the Scan() method, but instead RemoteProgressStatus:InitializeLifetimeService() which returns null. After returning from that method the exeption is thrown.

Is this a known problem and perhaps a work around? Thanks!

System.ArgumentException occurred Message="Object type cannot be converted to target type." Source="Mono.Addins" StackTrace: at Mono.Addins.Database.RemoteSetupDomain.Scan(IProgressStatus monitor, String registryPath, String startupDir, String scanFolder, String[] filesToIgnore) at Mono.Addins.Database.SetupDomain.Scan(IProgressStatus monitor, String registryPath, String startupDir, String scanFolder, String[] filesToIgnore) in C:\Users\Kiel\Projects\Mono.Addins \Mono.Addins\Mono.Addins.Database\SetupDomain.cs:line 43
InnerException:

Solution:

I moved AddinManager.Initialize(); and AddinManager.Registry.Update(null); to a method marked [ClassInitialize] versus trying to do this in the constructor and things are happy again.




回答2:


Finally I've managed to solve my problem:

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);

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;



来源:https://stackoverflow.com/questions/13493434/nonsense-argument-exception-calling-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!