-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathExtensions.cs
32 lines (29 loc) · 1.01 KB
/
Extensions.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
using System;
using System.Reflection;
namespace OmegaCrosspathing;
public static class ReflectionHelper
{
public static PropertyInfo GetPropertyInfo(Type type, string propertyName)
{
PropertyInfo propInfo = null;
do
{
propInfo = type.GetProperty(propertyName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
type = type.BaseType;
}
while (propInfo == null && type != null);
return propInfo;
}
public static object GetPropertyValue(this object obj, string propertyName)
{
if (obj == null)
throw new ArgumentNullException("obj");
Type objType = obj.GetType();
PropertyInfo propInfo = GetPropertyInfo(objType, propertyName);
if (propInfo == null)
throw new ArgumentOutOfRangeException("propertyName",
$"Couldn't find property {propertyName} in type {objType.FullName}");
return propInfo.GetValue(obj, null);
}
}