2 // PropertyInfoTest.cs - NUnit Test Cases for PropertyInfo
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // (C) 2004-2007 Gert Driesen
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Reflection;
32 using System.Runtime.InteropServices;
34 using NUnit.Framework;
36 namespace MonoTests.System.Reflection
39 public class PropertyInfoTest
42 public void GetAccessorsTest ()
44 Type type = typeof (TestClass);
45 PropertyInfo property = type.GetProperty ("ReadOnlyProperty");
47 MethodInfo [] methods = property.GetAccessors (true);
48 Assert.AreEqual (1, methods.Length, "#A1");
49 Assert.IsNotNull (methods [0], "#A2");
50 Assert.AreEqual ("get_ReadOnlyProperty", methods [0].Name, "#A3");
52 methods = property.GetAccessors (false);
53 Assert.AreEqual (1, methods.Length, "#B1");
54 Assert.IsNotNull (methods [0], "#B2");
55 Assert.AreEqual ("get_ReadOnlyProperty", methods [0].Name, "#B3");
57 property = typeof (Base).GetProperty ("P");
59 methods = property.GetAccessors (true);
60 Assert.AreEqual (2, methods.Length, "#C1");
61 Assert.IsNotNull (methods [0], "#C2");
62 Assert.IsNotNull (methods [1], "#C3");
63 Assert.IsTrue (HasMethod (methods, "get_P"), "#C4");
64 Assert.IsTrue (HasMethod (methods, "set_P"), "#C5");
66 methods = property.GetAccessors (false);
67 Assert.AreEqual (2, methods.Length, "#D1");
68 Assert.IsNotNull (methods [0], "#D2");
69 Assert.IsNotNull (methods [1], "#D3");
70 Assert.IsTrue (HasMethod (methods, "get_P"), "#D4");
71 Assert.IsTrue (HasMethod (methods, "set_P"), "#D5");
73 methods = property.GetAccessors ();
74 Assert.AreEqual (2, methods.Length, "#E1");
75 Assert.IsNotNull (methods [0], "#E2");
76 Assert.IsNotNull (methods [1], "#E3");
77 Assert.IsTrue (HasMethod (methods, "get_P"), "#E4");
78 Assert.IsTrue (HasMethod (methods, "set_P"), "#E5");
80 property = typeof (TestClass).GetProperty ("Private",
81 BindingFlags.NonPublic | BindingFlags.Instance);
83 methods = property.GetAccessors (true);
84 Assert.AreEqual (2, methods.Length, "#F1");
85 Assert.IsNotNull (methods [0], "#F2");
86 Assert.IsNotNull (methods [1], "#F3");
87 Assert.IsTrue (HasMethod (methods, "get_Private"), "#F4");
88 Assert.IsTrue (HasMethod (methods, "set_Private"), "#F5");
90 methods = property.GetAccessors (false);
91 Assert.AreEqual (0, methods.Length, "#G");
93 methods = property.GetAccessors ();
94 Assert.AreEqual (0, methods.Length, "#H");
97 property = typeof (TestClass).GetProperty ("PrivateSetter");
99 methods = property.GetAccessors (true);
100 Assert.AreEqual (2, methods.Length, "#H1");
101 Assert.IsNotNull (methods [0], "#H2");
102 Assert.IsNotNull (methods [1], "#H3");
103 Assert.IsTrue (HasMethod (methods, "get_PrivateSetter"), "#H4");
104 Assert.IsTrue (HasMethod (methods, "set_PrivateSetter"), "#H5");
106 methods = property.GetAccessors (false);
107 Assert.AreEqual (1, methods.Length, "#I1");
108 Assert.IsNotNull (methods [0], "#I2");
109 Assert.AreEqual ("get_PrivateSetter", methods [0].Name, "#I3");
111 methods = property.GetAccessors ();
112 Assert.AreEqual (1, methods.Length, "#J1");
113 Assert.IsNotNull (methods [0], "#J2");
114 Assert.AreEqual ("get_PrivateSetter", methods [0].Name, "#J3");
119 public void GetCustomAttributes ()
122 PropertyInfo p = typeof (Base).GetProperty ("P");
124 attrs = p.GetCustomAttributes (false);
125 Assert.AreEqual (1, attrs.Length, "#A1");
126 Assert.AreEqual (typeof (ThisAttribute), attrs [0].GetType (), "#A2");
127 attrs = p.GetCustomAttributes (true);
128 Assert.AreEqual (1, attrs.Length, "#A3");
129 Assert.AreEqual (typeof (ThisAttribute), attrs [0].GetType (), "#A4");
131 p = typeof (Base).GetProperty ("T");
133 attrs = p.GetCustomAttributes (false);
134 Assert.AreEqual (2, attrs.Length, "#B1");
135 Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B2");
136 Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B3");
137 attrs = p.GetCustomAttributes (true);
138 Assert.AreEqual (2, attrs.Length, "#B41");
139 Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B5");
140 Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B6");
142 p = typeof (Base).GetProperty ("Z");
144 attrs = p.GetCustomAttributes (false);
145 Assert.AreEqual (0, attrs.Length, "#C1");
146 attrs = p.GetCustomAttributes (true);
147 Assert.AreEqual (0, attrs.Length, "#C2");
151 public void GetCustomAttributes_Inherited ()
154 PropertyInfo p = typeof (Derived).GetProperty ("P");
156 attrs = p.GetCustomAttributes (false);
157 Assert.AreEqual (0, attrs.Length, "#A1");
158 attrs = p.GetCustomAttributes (true);
159 Assert.AreEqual (0, attrs.Length, "#A3");
161 p = typeof (Derived).GetProperty ("T");
163 attrs = p.GetCustomAttributes (false);
164 Assert.AreEqual (2, attrs.Length, "#B1");
165 Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B2");
166 Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B3");
167 attrs = p.GetCustomAttributes (true);
168 Assert.AreEqual (2, attrs.Length, "#B41");
169 Assert.IsTrue (HasAttribute (attrs, typeof (ThisAttribute)), "#B5");
170 Assert.IsTrue (HasAttribute (attrs, typeof (ComVisibleAttribute)), "#B6");
172 p = typeof (Derived).GetProperty ("Z");
174 attrs = p.GetCustomAttributes (false);
175 Assert.AreEqual (0, attrs.Length, "#C1");
176 attrs = p.GetCustomAttributes (true);
177 Assert.AreEqual (0, attrs.Length, "#C2");
181 public void IsDefined_AttributeType_Null ()
183 Type derived = typeof (Derived);
184 PropertyInfo pi = derived.GetProperty ("P");
187 pi.IsDefined ((Type) null, false);
189 } catch (ArgumentNullException ex) {
190 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
191 Assert.IsNull (ex.InnerException, "#3");
192 Assert.IsNotNull (ex.Message, "#4");
193 Assert.IsNotNull (ex.ParamName, "#5");
194 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
199 public void AccessorsReflectedType ()
201 PropertyInfo pi = typeof (Derived).GetProperty ("T");
202 Assert.AreEqual (typeof (Derived), pi.GetGetMethod ().ReflectedType);
203 Assert.AreEqual (typeof (Derived), pi.GetSetMethod ().ReflectedType);
206 [Test] // bug #399985
207 public void SetValue_Enum ()
209 TestClass t = new TestClass ();
210 PropertyInfo pi = t.GetType ().GetProperty ("Targets");
211 pi.SetValue (t, AttributeTargets.Field, null);
212 Assert.AreEqual (AttributeTargets.Field, t.Targets, "#1");
213 pi.SetValue (t, (int) AttributeTargets.Interface, null);
214 Assert.AreEqual (AttributeTargets.Interface, t.Targets, "#2");
217 public class ThisAttribute : Attribute
224 public virtual string P {
231 public virtual string T {
236 public virtual string Z {
244 public override string P {
250 static void RunTest (Type t, bool use_getter) {
251 var p = t.GetProperty ("Item");
252 var idx = p.GetIndexParameters ();
253 var m_args = t.GetMethod (use_getter ? "get_Item" : "set_Item").GetParameters ();
255 Assert.AreEqual (2, idx.Length, "#1");
257 Assert.AreEqual (typeof (double), idx [0].ParameterType, "#2");
258 Assert.AreEqual (p, idx [0].Member, "#3");
259 Assert.AreEqual ("a", idx [0].Name, "#4");
260 Assert.AreEqual (0, idx [0].Position, "#5");
261 Assert.AreEqual (m_args [0].MetadataToken, idx [0].MetadataToken, "#6");
262 Assert.AreEqual (ParameterAttributes.None, idx [0].Attributes, "#7");
264 Assert.AreEqual (typeof (string), idx [1].ParameterType, "#8");
265 Assert.AreEqual (p, idx [1].Member, "#9");
266 Assert.AreEqual ("b", idx [1].Name, "#10");
267 Assert.AreEqual (1, idx [1].Position, "#11");
268 Assert.AreEqual (m_args [1].MetadataToken, idx [1].MetadataToken, "#12");
269 Assert.AreEqual (ParameterAttributes.None, idx [1].Attributes, "#13");
271 var idx2 = p.GetIndexParameters ();
273 //No interning exposed
274 Assert.AreNotSame (idx, idx2, "#14");
275 Assert.AreNotSame (idx [0], idx2 [1], "#15");
279 public void GetIndexerReturnsObjectsBoundToTheProperty ()
285 public int this[double a, string b] {
291 public int this[double a, string b] {
301 public string Property {
302 get { return typeof (T).FullName; }
306 public int? nullable_field;
308 public int? NullableProperty {
309 get { return nullable_field; }
310 set { nullable_field = value; }
314 public void NullableTests ()
316 PropertyInfoTest t = new PropertyInfoTest ();
318 PropertyInfo pi = typeof(PropertyInfoTest).GetProperty("NullableProperty");
320 pi.SetValue (t, 100, null);
321 Assert.AreEqual (100, pi.GetValue (t, null));
322 pi.SetValue (t, null, null);
323 Assert.AreEqual (null, pi.GetValue (t, null));
327 public void Bug77160 ()
329 object instance = new A<string> ();
330 Type type = instance.GetType ();
331 PropertyInfo property = type.GetProperty ("Property");
332 Assert.AreEqual (typeof (string).FullName, property.GetValue (instance, null));
337 static bool HasAttribute (object [] attrs, Type attributeType)
339 foreach (object attr in attrs)
340 if (attr.GetType () == attributeType)
345 static bool HasMethod (MethodInfo [] methods, string name)
347 foreach (MethodInfo method in methods)
348 if (method.Name == name)
353 private class TestClass
355 private AttributeTargets _targets = AttributeTargets.Assembly;
357 public AttributeTargets Targets {
358 get { return _targets; }
359 set { _targets = value; }
362 public string ReadOnlyProperty {
363 get { return string.Empty; }
366 private string Private {
372 public string PrivateSetter {