1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // Federico Di Gregorio <fog@initd.org>
23 using System.Reflection;
25 using System.Linq.Expressions;
26 using NUnit.Framework;
28 namespace MonoTests.System.Linq.Expressions
31 public class ExpressionTest_Property
34 [ExpectedException (typeof (ArgumentNullException))]
35 public void Arg1Null ()
37 Expression.Property (null, "NoProperty");
41 [ExpectedException (typeof (ArgumentNullException))]
42 public void Arg2Null1 ()
44 Expression.Property (Expression.Constant (new MemberClass()), (string)null);
48 [ExpectedException (typeof (ArgumentNullException))]
49 public void Arg2Null2 ()
51 Expression.Property (Expression.Constant (new MemberClass()), (PropertyInfo)null);
55 [ExpectedException (typeof (ArgumentNullException))]
56 public void Arg2Null3 ()
58 Expression.Property (Expression.Constant (new MemberClass()), (MethodInfo)null);
62 [ExpectedException (typeof (ArgumentException))]
63 public void NoProperty ()
65 Expression.Property (Expression.Constant (new MemberClass()), "NoProperty");
69 public void InstanceProperty1 ()
71 MemberExpression expr = Expression.Property (Expression.Constant (new MemberClass()), "TestProperty1");
72 Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Property#01");
73 Assert.AreEqual (typeof (int), expr.Type, "Property#02");
74 Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.MemberClass).TestProperty1", expr.ToString(), "Property#03");
78 public void InstanceProperty2 ()
80 MemberExpression expr = Expression.Property (Expression.Constant (new MemberClass()), MemberClass.GetRoPropertyInfo());
81 Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Property#04");
82 Assert.AreEqual (typeof (int), expr.Type, "Property#05");
83 Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.MemberClass).TestProperty1", expr.ToString(), "Property#06");
87 public void InstanceProperty3 ()
89 MethodInfo mi = typeof(MemberClass).GetMethod("get_TestProperty1");
91 MemberExpression expr = Expression.Property (Expression.Constant (new MemberClass()), mi);
92 Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Property#07");
93 Assert.AreEqual (typeof (int), expr.Type, "Property#08");
94 Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.MemberClass).TestProperty1", expr.ToString(), "Property#09");
95 Assert.AreEqual (MemberClass.GetRoPropertyInfo(), expr.Member, "Property#10");
99 [ExpectedException (typeof (ArgumentException))]
100 public void StaticProperty1 ()
102 // This will fail because access to a static field should be created using a PropertyInfo and
103 // not an instance plus the field name.
104 Expression.Property (Expression.Constant (new MemberClass()), "StaticProperty");
108 public void StaticProperty2 ()
110 MemberExpression expr = Expression.Property (null, MemberClass.GetStaticPropertyInfo());
111 Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Property#11");
112 Assert.AreEqual (typeof (int), expr.Type, "Property#12");
113 Assert.AreEqual ("MemberClass.StaticProperty", expr.ToString(), "Property#13");
117 public void StaticProperty3 ()
119 MethodInfo mi = typeof(MemberClass).GetMethod("get_StaticProperty");
121 MemberExpression expr = Expression.Property (null, mi);
122 Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Property#14");
123 Assert.AreEqual (typeof (int), expr.Type, "Property#15");
124 Assert.AreEqual ("MemberClass.StaticProperty", expr.ToString(), "Property#16");
125 Assert.AreEqual (MemberClass.GetStaticPropertyInfo(), expr.Member, "Property#17");
129 public string Prop { get; set; }
131 public static string StatProp
133 get { return "StaticFoo"; }
138 public void TestCompileGetInstanceProperty ()
140 var p = Expression.Parameter (typeof (Foo), "foo");
141 var fooer = Expression.Lambda<Func<Foo, string>> (
142 Expression.Property (p, typeof (Foo).GetProperty ("Prop")), p).Compile ();
144 Assert.AreEqual ("foo", fooer (new Foo { Prop = "foo" }));
148 public void TestCompileGetStaticProperty ()
150 var sf = Expression.Lambda<Func<string>> (
151 Expression.Property (null, typeof (Foo).GetProperty (
152 "StatProp", BindingFlags.Public | BindingFlags.Static))).Compile ();
154 Assert.AreEqual ("StaticFoo", sf ());
162 set { slot = value; }
165 public Bar (string slot)
172 public void TestCompileGetInstancePropertyOnStruct ()
174 var p = Expression.Parameter (typeof (Bar), "bar");
175 var barer = Expression.Lambda<Func<Bar, string>> (
176 Expression.Property (p, typeof (Bar).GetProperty ("Prop")), p).Compile ();
178 Assert.AreEqual ("bar", barer (new Bar ("bar")));
181 public static int StaticProperty {
186 [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
187 [ExpectedException (typeof (ArgumentException))]
188 public void StaticPropertyWithInstanceArgument ()
190 Expression.Property (
191 Expression.Parameter (GetType (), "t"),
192 GetType ().GetProperty ("StaticProperty"));