您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Python中创建C#可空Int32(使用Python.NET)以使用可选的int参数调用C#方法

在Python中创建C#可空Int32(使用Python.NET)以使用可选的int参数调用C#方法

[编辑]

这已合并到pythonnet:

https://github.com/pythonnet/pythonnet/pull/460

我遇到了可空基元的相同问题- 在我看来Python.NET不支持这些类型。我通过在Python.Runtime.Converter.ToManagedValue()(\ src \ runtime \ converter.cs)中添加以下代码解决此问题

if( obType.IsGenericType && obType.GetGenericTypeDeFinition() == typeof(Nullable<>) )
{
    if( value == Runtime.PyNone )
    {
        result = null;
        return true;
    }
    // Set type to underlying type
    obType = obType.GetGenericArguments()[0];
}

我将这段代码放在下面

if (value == Runtime.PyNone && !obType.IsValueType) {
    result = null;
    return true;
}

https://github.com/pythonnet/pythonnet/blob/4df6105b98b302029e524c7ce36f7b3cb18f7040/src/runtime/converter.cs#L320

python 2022/1/1 18:27:57 有592人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶