question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get CLR type of Python class?

See original GitHub issue

Prerequisites

  • Are you running the latest version?
  • Are you reporting to the correct repository?
  • Did you perform a cursory search?

Description

So, we have as task getting CLR type of strongly typed class in IronPython, if possible, without invoking constructor. Example:

class Vehicle(Component):
    __metaclass__ = clrtype.ClrClass

    _Test = SpecialProperty[int]()

    @property
    @clrtype.accepts()
    @clrtype.returns(SpecialProperty[int])
    def Test(self):
        return self._Test

    def __init__(this, *args):
        return Component.__new__(type(this), *args)

Possible ways at now:

  1. Create object of this type and invoke GetType() from it. Example:
CompiledCode compiled = source.Compile();
compiled.Execute();
var pythonType = compiled.DefaultScope.GetVariable("Vehicle");
var obj = ops.CreateInstance(type, new object[] { Guid.NewGuid() });
var clrType = obj.GetType();
  1. Get base class type by accessing constructor. Example: var clrType = pythonType.__new__.Overloads.Targets[0].DeclaringType;
  2. Playing with “internal” methods of IronPython, but it’s hard.

Affiliated problems and possible fixes

  1. pythonType.GetType() throws System.MissingMemberException, but should return something like typeof(IronPython.NewTypes.IronPython.Runtime.Types.PythonType_2$2) or typeof(IronPython.Runtime.Types.PythonType).
  2. pythonType.__new__.Overloads.Function.DeclaringType return System.Object, but by logic, should return proper containing type.
  3. IronPython.Runtime.Types.PythonType should have reference to CLR type, if available.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sloziercommented, Mar 15, 2019

I’m not sure I follow what the issue is. pythonType is a CLR type.

var scope = engine.CreateScope();
engine.Execute(script, scope);
var pythonType = scope.GetVariable("Vehicle");
var type = (Type)pythonType;
var obj = Activator.CreateInstance(type, new object[] { Guid.NewGuid() });
Debug.Assert(obj.GetType() == type);
Debug.Assert(obj is Component);
Debug.Assert(pythonType(Guid.NewGuid()) is Component);
0reactions
sloziercommented, Mar 15, 2019

@DjArt Looks like I was incorrect, pythonType is PythonType is correct. But the PythonType can be cast to Type.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python clr.GetClrType() Examples - ProgramCreek.com
You can vote up the ones you like or vote down the ones you don't like, and go to the original project or...
Read more >
Type of an IronPython object - Stack Overflow
IronPython use the reflexion to handle .NET types. The .GetType() method calls the type from CLR. Its the same as calling clr.
Read more >
[Python.NET] Subclassing CLR types
Hi, subclassing CLR types does work as long as you hold a ... retrieve this object later through a method of the CLR...
Read more >
__clrtype__ Metaclasses: Customizing the Type Name - DevHawk
class ClrTypeMetaclass (type): def __clrtype__(cls): baseType ... Type that IronPython would have used as the underlying CLR type for the Python class if...
Read more >
CLR Inside Out: IronPython - Microsoft Learn
But just having dynamic types does not make a language dynamic. ... compare the amount of code that goes into simple C# or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found