简体   繁体   中英

Nullable type (C#) in Python for .NET

I'm currently working on a project in Python (for .NET) that calls functions from a C# .dll. There's a nullable type (double?) argument in one of the C# functions and apparently I have to edit the python source code ( Creating a C# Nullable Int32 within Python (using Python.NET) to call a C# method with an optional int argument ), which I'd prefer not to do in case other people will use this nor do I want to update the C# code to handle this case of conversion.

For now, I've tried the following:

nullable_double = System.Nullable[System.Double]()
func(nullable_double)

This gives me the following error:

ArgumentException: Object of type 'System.RuntimeType' cannot be converted to type 'System.Nullable`1[System.Double]'.

I also tried just passing in 'None' but that didn't work as well. Is there anyway I can do this without changing the python source code or the c# dll?

Please check, if the following is working with your func(nullable_double) :

import clr
import System

nullable_double = System.Nullable[System.Double](0) # just providing a number for initialization
print(nullable_double) # prints: 0
print(type(nullable_double)) # prints: <class 'System.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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