-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathPropertyInfoAccessor.cs
44 lines (36 loc) · 1.19 KB
/
PropertyInfoAccessor.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
using System;
using System.Reflection;
namespace YesSql.Serialization
{
public class PropertyInfoAccessor
{
private readonly IInvoker _invoker;
public PropertyInfoAccessor(PropertyInfo propertyInfo)
{
var delegateType = typeof(Func<,>).MakeGenericType(propertyInfo.DeclaringType, propertyInfo.PropertyType);
var d = propertyInfo.GetGetMethod().CreateDelegate(delegateType);
var invokerType = typeof(Invoker<,>).MakeGenericType(propertyInfo.DeclaringType, propertyInfo.PropertyType);
_invoker = Activator.CreateInstance(invokerType, new object[] { d }) as IInvoker;
}
public object Get(object obj)
{
return _invoker.Invoke(obj);
}
private interface IInvoker
{
object Invoke(object target);
}
private sealed class Invoker<T, TResult> : IInvoker
{
private readonly Func<T, TResult> _d;
public Invoker(Delegate d)
{
_d = (Func<T, TResult>)d;
}
public object Invoke(object target)
{
return _d((T)target);
}
}
}
}