[A] can not be cast to [B]

Hello @jurriaanfloor,
I thought the problem was solved.
What exactly are you trying to do and what problem do you face?
– Clemens

Hello @gl_karamba3d,

I am trying to make a .gha that reads the karamba model, collect specific data and write it to a json. But somehow after trying all the suggestions, like different .net framework, place the gha in different folders, naming the gha after karamba wrt alphabetical order, it keeps saying “Invalid cast: Model >> Model”.

  • Jurriaan

Hi @jurriaanfloor,
the problem that you report typically occurs when the karamba.gha or karamba.dll assembly are loaded twice. Normally this can be solved via the Rhino-command ‘GrasshopperDeveloperSettings’, unhooking ‘Memory load *.GHA…’ and restarting Rhino/Grasshopper.
The fact that the cast does not work shows that the assemblies are already loaded.
–Clemens

Hi @gl_karamba3d,

Unhooking the memory load doesn’t seem to make any difference. Also not when the .gha is placed in different locations.

  • Jurriaan

Hi @jurriaanfloor,
It could be that the ‘karamba.gha’- or ‘karamba.dll’-files which are referenced by Grasshopper are not the same as those you reference via VisualStudio or the GH C#-component.
In order to try that out: uninstall Karamba3D and remove all ‘karamba.gha’ and ‘karamba.dll’ files from your harddisk that still might lie around there. Then reinstall Karamba3D and update the references.
–Clemens

I also have this problem. We save karamba.gha as karambaGH.dll and reference it in our C# project
the error it is giving at the custom C# component even said type A and B are originated from the same source so I’m not too sure what is happening here
COFF option is untick and I’m sure there is no other karambaGH.dll on my PC
edit: naming it new_karambaGH.dll and save in another folder helps, but is there another way? Like to point Karamba to the bin/debug folder instead of copying this manually

Hello @user1986,
Grasshopper loads plug-ins in alphanumeric order of their names. Try this: make sure that the name of your plugin comes after ‘karamba’.
Do the examples of the K3D Scripting Guide work for you?
– Clemens

Thank you for your response
As I mentioned in the edit above, it works now but could you please explain why do we need to keep a copy of our gha build in that folder?
Ideally I just want to have it in our bin/debug so that we can deploy it to multiple PCs without having to go inside karamba folder and copy it manually. Cheers!

Dear @user1986,
this problem seems to be connected with the deeper mysteries of the .NET assembly loading mechanism. As far as I know .NET assembles are identified by their GUID. So in theory they get loaded into memory once and are then available for all other .NET applications. However I am not an expert on this. Maybe this information helps: How the Runtime Locates Assemblies - .NET Framework | Microsoft Docs.
– Clemens

1 Like