1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Linq.Expressions;
using Xunit;
using Xunit.Should;

namespace ExpressionTest
{
	public class PolymorphismExpressionTests
	{
		public abstract class Person
		{
			public abstract string NumberOfChildren { get; }
		}

		public class Child : Person
		{
			public override string NumberOfChildren
			{
				get { return "10"; }
			}
		}

		[Fact]
		public void CanGetDeclaringType()
		{
			Expression<Func<Child, object>> func = child => child.NumberOfChildren;

			var expression = (MemberExpression)func.Body;
			var type = func.Parameters[0].Type;
			var member = expression.Member;
			var actual = type.GetProperty(member.Name);

			member.DeclaringType.ShouldBe(typeof(Person));
			actual.DeclaringType.ShouldBe(typeof(Child));
		}
	}
}