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");
198 public class ThisAttribute : Attribute
205 public virtual string P {
212 public virtual string T {
217 public virtual string Z {
225 public override string P {
234 public string Property {
235 get { return typeof (T).FullName; }
239 public int? nullable_field;
241 public int? NullableProperty {
242 get { return nullable_field; }
243 set { nullable_field = value; }
247 public void NullableTests ()
249 PropertyInfoTest t = new PropertyInfoTest ();
251 PropertyInfo pi = typeof(PropertyInfoTest).GetProperty("NullableProperty");
253 pi.SetValue (t, 100, null);
254 Assert.AreEqual (100, pi.GetValue (t, null));
255 pi.SetValue (t, null, null);
256 Assert.AreEqual (null, pi.GetValue (t, null));
260 public void Bug77160 ()
262 object instance = new A<string> ();
263 Type type = instance.GetType ();
264 PropertyInfo property = type.GetProperty ("Property");
265 Assert.AreEqual (typeof (string).FullName, property.GetValue (instance, null));
269 static bool HasAttribute (object [] attrs, Type attributeType)
271 foreach (object attr in attrs)
272 if (attr.GetType () == attributeType)
277 static bool HasMethod (MethodInfo [] methods, string name)
279 foreach (MethodInfo method in methods)
280 if (method.Name == name)
285 private class TestClass
287 public string ReadOnlyProperty {
288 get { return string.Empty; }
291 private string Private {
297 public string PrivateSetter {