Grasshopper

algorithmic modeling for Rhino

How to embed external libraries into custom .gha component with C#?

Hello,

I want to create a custom .gha component with Visual Studio / C# that embeds external libraries, both created by third parties and by me. In this way, the component does not need every single required dll to be placed in the "special folders - component folder".

I have followed this approach that does not work and I don't know why:

- set the CopyLocal property to true for every .dll (except for System.dll otherwise the following passages wouldn't be possible)

- build the release version of the component

- change the extension of the generated .gha to .dll

- use ILMerge (GUI app) to merge all the required libraries, the principal being the one in the above step

- once the new dll has been generated, re-change its extension from .dll to .gha

When I try to drag the component in the GH canvas, nothing happens. What's wrong with this approach?

I have seen that another option would be to use reflection to dynamically load assemblies at runtime from the Resources folder - is it the only possible route then?

Views: 1157

Replies to This Discussion

I have never used ILMerge, no idea why it doesn't work, but I can't say I'm surprised it doesn't.

You have another option, which is to embed your secondary dlls as resources in your GHA, write them to disk the first time your GHA loads and then load them through code. I think you can still do this before your code actually calls any of them, for example in an GH_AssemblyPriority object.

Yet another option is to put all your assemblies and other files in an entirely different folder and have GH load it from there. It should be possible to do this using an RHI file, although I don't know yet whether they've made it work with just GHA files, or if it still requires an RHP file as well.

An easy way to have GH load a GHA from a custom folder is to put a textfile with a GHLINK extension in the SpecialFolder/Components Folder. This file can contain any number of folder locations from where GH is expected to load GHA files (one folder uri per line, lines starting with # are ignored).

Well, the GH_AssemblyPriority class seems to work like a charm! Only time will tell though...

I have added the assemblies to the project both as References and "Existing item". These last must have the property "Build Action" equal to "Embedded Resource". Then, they can be loaded at runtime via the PriorityLoad() method. This link has helped a lot:

https://www.codeproject.com/Articles/528178/Load-DLL-From-Embedded-...

Thanks much David!

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service