Skip to content

“TypeError: Cannot get managed object” due to getting wrong type info from .NET structs #488

Closed
@DfM7

Description

@DfM7

Environment

  • Pythonnet version: 2.3.0
  • Python version: Python 3.6.1 64-bit
  • Operating System: Windows 10 1703 64-bit

Details

pythonnet fails to get the right type from the struct.

C# code:

namespace PyNetFails
{
    public struct A
    {
        public int X;
        public int Y;
        public uint V;
    }
}

Then compile it as class library named PyNetFails and run the following Python code:

import clr
clr.AddReference('PyNetFails.dll')
from PyNetFails import A
a = A()
print(type(a.V))

It produces: <class 'int'>, although the type should be System.UInt32.
Then try to run afterwards the following code:

from System import UInt32
v = UInt32(42)
print(v >= a.V)
  • Traceback:
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    print(v >= a.V)
TypeError: Cannot get managed object

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions