/
PropertyReader.cs
53 lines (48 loc) · 1.61 KB
/
PropertyReader.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace NLog.Web.Internal
{
internal class PropertyReader
{
/// <summary>
/// Get value of a property
/// </summary>
/// <param name="key">key</param>
/// <param name="getVal">function to get a value with this key</param>
/// <param name="evaluateAsNestedProperties">evaluate <paramref name="key"/> as a nested property path. E.g. A.B is property B inside A.</param>
/// <returns>value</returns>
public static object GetValue(string key, Func<string, object> getVal, bool evaluateAsNestedProperties)
{
if (key == null)
{
return null;
}
object value;
if (evaluateAsNestedProperties)
{
var path = key.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
value = getVal(path.First());
foreach (var property in path.Skip(1))
{
var propertyInfo = GetPropertyInfo(value, property);
value = propertyInfo.GetValue(value, null);
}
}
else
{
value = getVal(key);
}
return value;
}
private static PropertyInfo GetPropertyInfo(object value, string propertyName)
{
#if !NETSTANDARD_1plus
return value.GetType().GetProperty(propertyName);
#else
return value.GetType().GetTypeInfo().GetDeclaredProperty(propertyName);
#endif
}
}
}