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_Field
34 [ExpectedException (typeof (ArgumentNullException))]
35 public void Arg1Null ()
37 Expression.Field (null, "NoField");
41 [ExpectedException (typeof (ArgumentNullException))]
42 public void Arg2Null1 ()
44 Expression.Field (Expression.Constant (new MemberClass()), (string)null);
48 [ExpectedException (typeof (ArgumentNullException))]
49 public void Arg2Null2 ()
51 Expression.Field (Expression.Constant (new MemberClass()), (FieldInfo)null);
55 [ExpectedException (typeof (ArgumentException))]
56 public void NoField ()
58 Expression.Field (Expression.Constant (new MemberClass()), "NoField");
62 public void InstanceField ()
64 MemberExpression expr = Expression.Field (Expression.Constant (new MemberClass()), "TestField1");
65 Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Field#01");
66 Assert.AreEqual (typeof (int), expr.Type, "Field#02");
67 Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.MemberClass).TestField1", expr.ToString(), "Field#03");
71 [ExpectedException (typeof (ArgumentException))]
72 public void StaticField1 ()
74 // This will fail because access to a static field should be created using a FieldInfo and
75 // not an instance plus the field name.
76 Expression.Field (Expression.Constant (new MemberClass()), "StaticField");
80 public void StaticField2 ()
82 MemberExpression expr = Expression.Field (null, MemberClass.GetStaticFieldInfo());
83 Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Field#07");
84 Assert.AreEqual (typeof (int), expr.Type, "Field#08");
85 Assert.AreEqual ("MemberClass.StaticField", expr.ToString(), "Field#09");
89 [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
90 [ExpectedException (typeof (ArgumentException))]
91 public void StaticFieldWithInstanceArgument ()
94 Expression.Parameter (GetType (), "t"),
95 GetType ().GetField ("foo"));
98 public static string foo = "foo";
101 public void CompileStaticField ()
103 var foo = Expression.Lambda<Func<string>> (
104 Expression.Field (null, GetType ().GetField (
105 "foo", BindingFlags.Static | BindingFlags.Public))).Compile ();
107 Assert.AreEqual ("foo", foo ());
120 public void CompileInstanceField ()
122 var p = Expression.Parameter (typeof (Bar), "bar");
123 var baz = Expression.Lambda<Func<Bar, string>> (
124 Expression.Field (p, typeof (Bar).GetField (
125 "baz", BindingFlags.Public | BindingFlags.Instance)), p).Compile ();
127 Assert.AreEqual ("baz", baz (new Bar ()));
130 public struct Gazonk {
133 public Gazonk (string tzap)
140 public void CompileStructInstanceField ()
142 var p = Expression.Parameter (typeof (Gazonk), "gazonk");
143 var gazonker = Expression.Lambda<Func<Gazonk, string>> (
144 Expression.Field (p, typeof (Gazonk).GetField ("Tzap")), p).Compile ();
146 Assert.AreEqual ("bang", gazonker (new Gazonk ("bang")));