/
PropertyValueGetter.cs
74 lines (61 loc) · 2.11 KB
/
PropertyValueGetter.cs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace FubuCore.Reflection
{
public class PropertyValueGetter : IValueGetter
{
private readonly PropertyInfo _propertyInfo;
public PropertyValueGetter(PropertyInfo propertyInfo)
{
_propertyInfo = propertyInfo;
}
public PropertyInfo PropertyInfo { get { return _propertyInfo; } }
public object GetValue(object target)
{
return _propertyInfo.GetValue(target, null);
}
public string Name
{
get { return _propertyInfo.Name; }
}
public Type DeclaringType
{
get { return _propertyInfo.DeclaringType; }
}
public Type ValueType
{
get { return _propertyInfo.PropertyType; }
}
public Expression ChainExpression(Expression body)
{
var memberExpression = Expression.Property(body, _propertyInfo);
if (!_propertyInfo.PropertyType.IsValueType)
{
return memberExpression;
}
return Expression.Convert(memberExpression, typeof (object));
}
public void SetValue(object target, object propertyValue)
{
_propertyInfo.SetValue(target, propertyValue, null);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof(PropertyValueGetter)) return false;
return Equals((PropertyValueGetter) obj);
}
public bool Equals(PropertyValueGetter other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other._propertyInfo.PropertyMatches(_propertyInfo);
}
public override int GetHashCode()
{
return (_propertyInfo != null ? _propertyInfo.GetHashCode() : 0);
}
}
}