1

I'm receiving an ArgumentException "Object type cannot be converted to target type" but it doesn't make very sense to me.You can see the exception in this image

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?

Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
  • 2
    I guess the problem is inside Scan method. Is it a method you wrote yourself ? This is likely due to some reflection error – Kek Nov 21 '12 at 12:40
  • if I press "step into" the exception is thrown before it enters inside the method body, so I think that the problem is somewhere in the argument types. I don't write the method by myself, this is mono.addins source code: http://monoaddins.codeplex.com/releases/view/96359 – Tobia Zambon Nov 21 '12 at 12:43
  • Have you tried looking at the StackTrace of the exception? – dtb Nov 21 '12 at 12:44
  • this is the stack trace: at Mono.Addins.Database.RemoteSetupDomain.Scan(IProgressStatus monitor, String registryPath, String startupDir, String addinsDir, String databaseDir, String scanFolder, String[] filesToIgnore) at Mono.Addins.Database.SetupDomain.Scan(IProgressStatus monitor, AddinRegistry registry, String scanFolder, String[] filesToIgnore) in d:\Desktop\Mono.Addins-source-1.0\Mono.Addins\Mono.Addins.Database\SetupDomain.cs:line 44 – Tobia Zambon Nov 21 '12 at 12:45
  • Sorry I don't know how to format it – Tobia Zambon Nov 21 '12 at 12:46

2 Answers2

0

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.

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
  • no luck, still the same error. My project is not a test project, so maybe something similar to [ClassInitialize] for normal projects should works? I don't know what does ClassInitialize.. – Tobia Zambon Nov 21 '12 at 15:19
0

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;

Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69