# -*- coding: utf-8 -*-"""Test clrmethod and clrproperty support for calling methods and getting/setting python properties from CLR."""import Python.Test as Testimport Systemimport pytestimport clrclass ExampleClrClass(System.Object):__namespace__ = "PyTest"def __init__(self):self._x = 3@clr.clrmethod(int, [int])def test(self, x):return x*2def get_X(self):return self._xdef set_X(self, value):self._x = valueX = clr.clrproperty(int, get_X, set_X)@clr.clrproperty(int)def Y(self):return self._x * 2def test_set_and_get_property_from_py():"""Test setting and getting clr-accessible properties from python."""t = ExampleClrClass()assert t.X == 3assert t.Y == 3 * 2t.X = 4assert t.X == 4assert t.Y == 4 * 2def test_set_and_get_property_from_clr():"""Test setting and getting clr-accessible properties from the clr."""t = ExampleClrClass()assert t.GetType().GetProperty("X").GetValue(t) == 3assert t.GetType().GetProperty("Y").GetValue(t) == 3 * 2t.GetType().GetProperty("X").SetValue(t, 4)assert t.GetType().GetProperty("X").GetValue(t) == 4assert t.GetType().GetProperty("Y").GetValue(t) == 4 * 2def test_set_and_get_property_from_clr_and_py():"""Test setting and getting clr-accessible properties alternatingly from the clr and from python."""t = ExampleClrClass()assert t.GetType().GetProperty("X").GetValue(t) == 3assert t.GetType().GetProperty("Y").GetValue(t) == 3 * 2assert t.X == 3assert t.Y == 3 * 2t.GetType().GetProperty("X").SetValue(t, 4)assert t.GetType().GetProperty("X").GetValue(t) == 4assert t.GetType().GetProperty("Y").GetValue(t) == 4 * 2assert t.X == 4assert t.Y == 4 * 2t.X = 5assert t.GetType().GetProperty("X").GetValue(t) == 5assert t.GetType().GetProperty("Y").GetValue(t) == 5 * 2assert t.X == 5assert t.Y == 5 * 2def test_method_invocation_from_py():"""Test calling a clr-accessible method from python."""t = ExampleClrClass()assert t.test(41) == 41*2def test_method_invocation_from_clr():"""Test calling a clr-accessible method from the clr."""t = ExampleClrClass()assert t.GetType().GetMethod("test").Invoke(t, [37]) == 37*2
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。