80 lines
2.2 KiB
C#
80 lines
2.2 KiB
C#
|
using System;
|
|||
|
using System.Reflection;
|
|||
|
|
|||
|
namespace RobloxFiles.Utility
|
|||
|
{
|
|||
|
// This is a lazy helper class to disambiguate between FieldInfo and PropertyInfo
|
|||
|
internal class ImplicitMember
|
|||
|
{
|
|||
|
private static BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase;
|
|||
|
private object member;
|
|||
|
|
|||
|
private ImplicitMember(FieldInfo field) { member = field; }
|
|||
|
private ImplicitMember(PropertyInfo prop) { member = prop; }
|
|||
|
|
|||
|
public static ImplicitMember Get(Type type, string name)
|
|||
|
{
|
|||
|
var field = type.GetField(name, flags);
|
|||
|
var prop = type.GetProperty(name, flags);
|
|||
|
|
|||
|
if (field != null)
|
|||
|
return new ImplicitMember(field);
|
|||
|
else if (prop != null)
|
|||
|
return new ImplicitMember(prop);
|
|||
|
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public Type MemberType
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
Type result = null;
|
|||
|
|
|||
|
if (member is FieldInfo)
|
|||
|
{
|
|||
|
var field = member as FieldInfo;
|
|||
|
result = field.FieldType;
|
|||
|
}
|
|||
|
else if (member is PropertyInfo)
|
|||
|
{
|
|||
|
var prop = member as PropertyInfo;
|
|||
|
result = prop.PropertyType;
|
|||
|
}
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public object GetValue(object obj)
|
|||
|
{
|
|||
|
if (member is FieldInfo)
|
|||
|
{
|
|||
|
var field = member as FieldInfo;
|
|||
|
return field.GetValue(obj);
|
|||
|
}
|
|||
|
else if (member is PropertyInfo)
|
|||
|
{
|
|||
|
var prop = member as PropertyInfo;
|
|||
|
return prop.GetValue(obj);
|
|||
|
}
|
|||
|
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public void SetValue(object obj, object value)
|
|||
|
{
|
|||
|
if (member is FieldInfo)
|
|||
|
{
|
|||
|
var field = member as FieldInfo;
|
|||
|
field.SetValue(obj, value);
|
|||
|
}
|
|||
|
else if (member is PropertyInfo)
|
|||
|
{
|
|||
|
var prop = member as PropertyInfo;
|
|||
|
prop.SetValue(obj, value);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|