ironpython使用dictionary_Ironpython引用C#数据结构

虽然Ironpython可以使用.net中的对象,但是在真正使用的时候,还是有一些需要注意的地方,这里列出平时整理出来的,供参看

1. 使用.net中的List

from System.Collections.Generic import List, Dictionary

int_list = List[int]()

同样Byte类型的值的定义

from System import Byte

b = Byte(1)

2.C# lambada

>>> from System.Collections.Generic import IEnumerable, List

>>> list = List[int]([1, 2, 3])

>>> import clr

>>> clr.AddReference("System.Core")

>>> from System.Linq import Enumerable

>>> Enumerable.Any(list,lambda x:x<2)

True

>>> Enumerable.Any(list,lambda x:x>2)

True

>>> Enumerable.Any(list,lambda x:x>4)

False

3.C#中的byte[]数组,在Ironpython中这样表示bbyte = Array[Byte]

当然,首先需要import数组和Byte from System import Array,Byte

还有一种方式是通过System.Type来达到

bbtype = System.Type.GetType('System.Byte')

b_buffer = System.Array.CreateInstance(bbtype, 1024)

4.ref 和 out参数类型

>>> from System.Collections.Generic import Dictionary

>>> d = { "a":100.1, "b":200.2, "c":300.3 }

>>> d = Dictionary[str, float](d)

>>> d.TryGetValue("b")

(True, 200.2)

>>> import clr

>>> r = clr.Reference[float]()

>>> d.TryGetValue("b", r)

True

>>> r.Value

200.2

>>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值