繁体   English   中英

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

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

我正在通过PythonNet库中的clr模块从 Python 3.10 脚本调用一组 C# 函数。 其中一个函数返回一个System.Reflection.Pointer类型的指针,它指向一个float值数组。

对于应该如何获取或访问System.Reflection.Pointer变量应该指向的实际float组,我感到很困惑。

在我的 Visual Studio 2022 IDE 中,我可以看到指针变量有几个 class 函数,例如ToString()Unbox() ,但是这些函数都没有给我所需的float组。

访问 Python 中System.Reflection.Pointer变量指向的数据的正确方法是什么?

感谢您阅读我的帖子,感谢任何指导。

仅使用 Python.NET API 无法做到这一点。 创建一个 C# 转换器(作为class 库并在 Python 中调用它,如下例所示。

在 C# 中,这样做。

using System.Reflection;

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

在 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