Creating a C # Nullable Int32 in Python (using Python.NET) to invoke a C # method with an optional int argument

I am using Python.NET to load a C # assembly to invoke C # code from Python. This works pretty cleanly, however I am having a problem calling a method that looks like this:

Method inside Our.Namespace.Proj.MyRepo:

OutputObject GetData(string user, int anID, int? anOptionalID= null)

I can call the method for the case when an additional third argument is present, but I cannot understand what to pass to the third argument so that it corresponds to the zero case.

import clr
clr.AddReference("Our.Namespace.Proj")
import System
from Our.Namespace.Proj import MyRepo

_repo = MyRepo()

_repo.GetData('me', System.Int32(1), System.Int32(2))  # works!

_repo.GetData('me', System.Int32(1))  # fails! TypeError: No method matches given arguments

_repo.GetData('me', System.Int32(1), None)  # fails! TypeError: No method matches given arguments

iPython Notebook indicates that the last argument must be of type:

System.Nullable`1[System.Int32]

Just not sure how to create an object that matches the Null case.

, Null? , Python None , .

+4
3

[EDIT]

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

+2

,

_repo.GetData('me', System.Int32(1), System.Nullable[System.Int32]())

, Nullable, Nullable Int32 new System.Nullable<int>() #.

, , , #; .

+2

You must pass an argument to a generic function System.Nullable[System.Int32](0)

+1
source

All Articles