How to use C# Class Libraries (dll) in Python

In the end it was a rather stupid mistake:
I took the wrong type of ClassLibrary. Instead of .NET Framework I chose .NET Core.

It works as described by you, @djordje @jdhill @AndersDeleuran.
Thanks a lot!

3 Likes

@djordje @AndersDeleuran @jdhill
I just tried the same thing in Rhino8 WIP and it returns the following message:

'RhinoTools' object has no attribute 'RhinoToolsGeometry'
Traceback:
line 9, in <module>, "C:\Users\TSTOLT~1\AppData\Local\Temp\TempScript.py"

No idea what is wrong here.

Rhino 8 changed from the NET framework to new NET X. I don’t know to which version, but you can decompile rhinocommon.dll using DNSpy or ILSpy, go under References and either search for System.Runtime (Net Core, Net 5+) or mscorlib (Net Framework) and check the version. If it tells 6.0.0.0 in the System.Runtime, it means you need to reference NET 6. Or you write your library using the NetStandard, then it doesn’t matter. But this requires proper encapsulation of your code-base and its a bit up the problems you solve…

1 Like

So I am just testing this in Rhino8 Beta.

It basically works fine, but when I want to check this method

    public string TestMethod(string nameA, string nameB)
    {
        return string.Concat(nameA, nameB);
    }

I get the following…

image

You can’t call a non-static method like this

you need to instantiate the class containing it like so:
myInstance = MyObject()
myInstance.TestMethod(nameA, nameB)

or you make it static… (public static string TestMethod…)
MyObject.TestMethod(nameA, nameB)

or you use the non-static method, and call it in alternative way (only Python)
MyObject.TestMethod(myInstance, nameA, nameB)

In case you don’t know what static means. In classes a method should be non-static if it modifies the data exclusive for one instance. It gives access to class fields and properties, so that you don’t need to pass them in. A non-static method can modify the state of the instance.
A static method is independent from an instance. Its a common function thematically belonging to the object, but its not relying on its data. Static functions are usually helper or factory methods. Static functions are easier to test. So you should always try to extract static logic/content out of something data dependent.

1 Like

Hi @TomTom,

oh my gosh, you are right, I used it like it is static!

Sorry, my bad!