问题
I hope to load .net dll in ironpython.
But one of static functions in .net dll, has some Named and Optional Arguments.
like, Draw(weight:w,height:h, Area=1)
Only can I use full arguments?
回答1:
Named and optional parameters are fully supported. .NET has had these for a long time for VB.NET support and so IronPython has supported that same way to do them since the beginning. The new C# syntax maps to the same underlying metadata as the old VB support.
For calling you use f(x = 42) which is Python's named parameter syntax. For optional parameters you can just leave them out. In your example case you can probably do Draw(weight, height) and leave Area out. Or you can call w/ weight and height as named parameters and leave Area out.
The underlying .NET meta data that IronPython looks for is either the OptionalAttribute or DefaultParameterValueAttribute. For optional we pass in default(T) unless the type is object in which case we pass in Missing.Value. This generally matches how reflection calls these APIs as well.
回答2:
As this SO question says, the new named arguments of .NET are not supported in IronPython (which uses named arguments to constructors it calls to set properties instead).
For optional ones, as this post says,
for optional parameters, like the arguments to the Address indexer, you can use Missing.Value to use the default.
after, of course, from System.Reflection import Missing
.
来源:https://stackoverflow.com/questions/3384989/can-i-use-named-and-optional-arguments-in-ironpython