Translating C# syntax to IronPython

Could anyone please show me how would this:

ComInterface ci = (ComInterfaceClass)Marshal.CreateWrapperOfType( GenericType, typeof(ComInterfaceClass))

look like in IronPython?

I am particularly confused by the (ComInterfaceClass)Marshal... Why is this put there and why in brackets?

Thanks in advance.

thats some explicit casting happening, you can look here for an idea on how to do this in python i guess for reference

1 Like


So This:

String i = (String)d;

is equal to this:

String i = d.ToString();

I understand the latter, the former was quite confusing. I guess C# peculiarity to make python and noob programmers to bang their heads in the wall.


is it ToString(d) or d.ToString()?? :thinking:

here it’s explained in a short and precise manner.
It would be Convert.ToString(d) or d…ToString() if the data type has that method.

I guess C# peculiarity to make python and noob programmers to bang their heads in the wall.

It’s actually helping you by making things easier to understand once you overcome the initial hurdle that you have to follow some ground rules. python makes things way more confusing in that respect.

1 Like

Com interfaces work generic, you get an instance without a clue whats in there. But you know from reading the api documentation.
In order to work with it further you need to assign (cast) it to the right type, at least in a strong typed language like C#. Now putting the type in front of the type with braces, is a so called C cast. If it fails it throws, and if uncatched it will crash your application. In Python you can do the same mytype(myinstance) -> Python (mytype)myinstance -> C#. Don‘t know if that’s actually working the same way.

C# offers another way in casting by using the as operator, which does not throw but returns a null value instead. But working with null (None=>Python)values is problematic as well.

Strings are a special case, because any class or struct can override a ToString() method, which, if overwritten, determines the string conversion.

In order to convert value types to other value types there is a Convert class which does that. This is where C and C# differs. Value types are float, double , byte, char etc pp.

Casting is not necessarily a conversion, it mainly is used to assign a generic or base object the right type. It works if the memory footprint matches if not it throws. If you get a GeometryBase object, you can try to cast it to a Brep, it will do if the instance is of that type, but it will throw if its a Mesh object for instance.

1 Like

Thanks a lot for this explanation @TomTom,

This whole COM story is crazy, I get to a point that I can dir() some class, type and I get it’s attributes but when I try SomeType.SomeAttribute or SomeType.SomeMethod I get an error. It’s really frustrating because the error says there’s no such attribute.

At the end just to be completely sure I understand what you said: by Throws do you mean throws and exception? An Error?

Thanks @rgr,

This is a nice video. I like Indians when they lead some course or training. For some reason I understand every word :slight_smile:

One question then. If explicit casting leads to a data loss, why would people use it with COM objects where almost everything is IUnknown derived?

correct. “throw” in C# is “raise” in Python.