我可以在ironpython中使用命名和可选参数吗 [英] Can I use Named and Optional Arguments in ironpython

查看:111
本文介绍了我可以在ironpython中使用命名和可选参数吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在ironpython中加载.net dll.

I hope to load .net dll in ironpython.

但是.net dll中的静态函数之一具有一些命名参数"和可选参数".

But one of static functions in .net dll, has some Named and Optional Arguments.

like,Draw(weight:w,height:h,Area = 1)

like, Draw(weight:w,height:h, Area=1)

我只能使用完整的参数吗?

Only can I use full arguments?

推荐答案

完全支持命名和可选参数. .NET为VB.NET支持已经有很长时间了,因此IronPython从一开始就以相同的方式支持它们.新的C#语法映射到与旧的VB支持相同的基础元数据.

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.

要进行调用,请使用f(x = 42),这是Python的命名参数语法.对于可选参数,您可以将其省略.在您的示例案例中,您可能可以执行Draw(weight,height)并将Area留在外面.或者,您可以将带体重和身高的w/作为命名参数,而将Area留在外面.

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.

IronPython查找的基础.NET元数据是OptionalAttribute或DefaultParameterValueAttribute.对于可选参数,我们传入default(T),除非类型是对象,在这种情况下,我们传入Missing.Value.通常,这也与反射如何调用这些API相匹配.

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.

这篇关于我可以在ironpython中使用命名和可选参数吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆