I hope to load .net dll in ironpython.
But one of static functions in .net dll, has some Named and Optional Arg开发者_如何转开发uments.
like, Draw(weight:w,height:h, Area=1)
Only can I use full arguments?
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.
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
.
精彩评论