简体   繁体   English

访问“System.Reflection.Pointer”的内容? (蟒蛇 3.10)

[英]Accessing Contents of "System.Reflection.Pointer"? (Python 3.10)

I'm calling a set of C# functions from a Python 3.10 script via the clr module from the PythonNet library.我正在通过PythonNet库中的clr模块从 Python 3.10 脚本调用一组 C# 函数。 One of those functions returns a pointer of type System.Reflection.Pointer , that points to an array of float values.其中一个函数返回一个System.Reflection.Pointer类型的指针,它指向一个float值数组。

I'm quite a bit confused as to how I'm exactly supposed to acquire or access the actual float array that the System.Reflection.Pointer variable is supposedly pointing to.对于应该如何获取或访问System.Reflection.Pointer变量应该指向的实际float组,我感到很困惑。

In my Visual Studio 2022 IDE, I can see that the pointer variable has a few class functions, such as ToString() or Unbox() , however none of those give me the desired float array.在我的 Visual Studio 2022 IDE 中,我可以看到指针变量有几个 class 函数,例如ToString()Unbox() ,但是这些函数都没有给我所需的float组。

What is the proper way of accessing the data pointed to by a System.Reflection.Pointer variable in Python?访问 Python 中System.Reflection.Pointer变量指向的数据的正确方法是什么?

Thanks for reading my post, any guidance is appreciated.感谢您阅读我的帖子,感谢任何指导。

You can't do that with only Python.NET APIs.仅使用 Python.NET API 无法做到这一点。 Create a C# converter(as a class library and call it in Python, like the following example.创建一个 C# 转换器(作为class 库并在 Python 中调用它,如下例所示。

In C#, do like this.在 C# 中,这样做。

using System.Reflection;

namespace Example;
public class Converter
{
    public static unsafe IntPtr PointerToIntPtr(Pointer p)
    {
        return (IntPtr)Pointer.Unbox(p);
    }
}

In Python, do like this.在 Python 中,这样做。

import clr

clr.AddReference('path to the .NET dll')
import Example
...
# The ptr is assumed to be an instance of System.Reflection.Pointer
addr = Example.Converter.PointerToIntPtr(ptr).ToInt64()
# The addr will be an int.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM