@@ -65,3 +65,66 @@ def test_dynamic_and_native_members_coexist():
6565 assert obj .answer == 42
6666 assert obj .Multiplier == 2
6767 assert obj .Multiply (10 ) == 20
68+
69+
70+ @pytest .mark .parametrize ("obj" , [DynamicMappingObject (), ExpandoObject ()])
71+ def test_set_and_get_dynamic_property (obj ):
72+ """Test that setting and getting dynamic properties goes through DLR binder."""
73+ # Get initial value (should be None for non-existent property)
74+ assert not hasattr (obj , "MyProp" )
75+
76+ # Set a dynamic property to a value
77+ obj .MyProp = 42
78+ assert obj .MyProp == 42
79+
80+ # Set to None and verify it stays None through DLR
81+ obj .MyProp = None
82+ assert obj .MyProp is None
83+
84+ # Set to another value and verify
85+ obj .MyProp = "hello"
86+ assert obj .MyProp == "hello"
87+
88+
89+ def test_update_dynamic_value ():
90+ """Setting from Python must update the backing dynamic store in C#."""
91+ obj = DynamicMappingObject ()
92+ obj .SetDynamicValue ("TestProp" , "initial" )
93+ assert obj .TestProp == "initial"
94+
95+ obj .TestProp = None
96+
97+ assert obj .TestProp is None
98+ assert obj .GetDynamicValue ("TestProp" ) is None
99+
100+
101+ def test_derive_from_dynamic_class ():
102+ class MyMappingObject (DynamicMappingObject ):
103+ __namespace__ = "PythonNetTest"
104+
105+ def __init__ (self ):
106+ self ._custom = 0
107+
108+ @property
109+ def custom_property (self ):
110+ return self ._custom
111+
112+ @custom_property .setter
113+ def custom_property (self , i ):
114+ self ._custom += i
115+
116+
117+ obj = MyMappingObject ()
118+ with pytest .raises (AttributeError ):
119+ x = obj .unknown_property
120+
121+ assert obj .custom_property == 0
122+
123+ obj .custom_property = 5
124+ assert obj .custom_property == 5
125+
126+ obj .custom_property = 5
127+ assert obj .custom_property == 10
128+
129+ obj .other_property = None
130+ assert obj .other_property is None
0 commit comments