tags:

views:

271

answers:

2

I'm interfacing with a .NET API in IronPython. The API is returning an object of the wrong type (some kind of generic object). I suspect that the problem is not showing up in their C# code because the type declaration when the object is constructed is forcing the returned object to the correct type. Is it possible to typecast an .NET object in IronPython? I think this would do the trick.

A: 

To force a conversion you can do:

import clr
convertedObject = clr.Convert(someObject, someType)

This will search for and run implicit/explicit conversions if one exists.

Dino Viehland
I couldn't get this to work. I couldn't find a "Convert" method in my IronPython clr.
Phil Smyth
A: 

I had a similar problem on a project a few months ago. This was my fix:

import clr

clr.GetPythonType(x)

x can be a .NET type or a type that is in a dll file that you have imported using clr.

I am not a C# programmer, but I have been told by C# programmer colleagues that this code in C# would be:

typeof(x)

Hope this helps

inspectorG4dget