-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathPropertyFetcher.cs
90 lines (77 loc) · 3.36 KB
/
PropertyFetcher.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
namespace Microsoft.ApplicationInsights.Common
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
/// <summary>
/// Efficient implementation of fetching properties of anonymous types with reflection.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "This is used substantially")]
internal class PropertyFetcher
{
private readonly string propertyName;
private volatile PropertyFetch innerFetcher;
public PropertyFetcher(string propertyName)
{
this.propertyName = propertyName;
}
public object Fetch(object obj)
{
PropertyFetch fetch = this.innerFetcher;
Type objType = obj?.GetType();
if (fetch == null || fetch.Type != objType)
{
this.innerFetcher = fetch = PropertyFetch.FetcherForProperty(objType, objType?.GetTypeInfo()?.GetDeclaredProperty(this.propertyName));
}
return fetch?.Fetch(obj);
}
// see https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs
private class PropertyFetch
{
public PropertyFetch(Type type)
{
this.Type = type;
}
/// <summary>
/// Gets the type of the object that the property is fetched from. For well-known static methods that
/// aren't actually property getters this will return null.
/// </summary>
internal Type Type { get; }
/// <summary>
/// Create a property fetcher from a .NET Reflection PropertyInfo class that
/// represents a property of a particular type.
/// </summary>
public static PropertyFetch FetcherForProperty(Type type, PropertyInfo propertyInfo)
{
if (propertyInfo == null)
{
// returns null on any fetch.
return new PropertyFetch(type);
}
var typedPropertyFetcher = typeof(TypedFetchProperty<,>);
var instantiatedTypedPropertyFetcher = typedPropertyFetcher.GetTypeInfo().MakeGenericType(
propertyInfo.DeclaringType, propertyInfo.PropertyType);
return (PropertyFetch)Activator.CreateInstance(instantiatedTypedPropertyFetcher, type, propertyInfo);
}
/// <summary>
/// Given an object, fetch the property that this propertyFetch represents.
/// </summary>
public virtual object Fetch(object obj)
{
return null;
}
private class TypedFetchProperty<TObject, TProperty> : PropertyFetch
{
private readonly Func<TObject, TProperty> propertyFetch;
public TypedFetchProperty(Type type, PropertyInfo property) : base(type)
{
this.propertyFetch = (Func<TObject, TProperty>)property.GetMethod.CreateDelegate(typeof(Func<TObject, TProperty>));
}
public override object Fetch(object obj)
{
return this.propertyFetch((TObject)obj);
}
}
}
}
}