Switch-Toolbox/Switch_Toolbox_Library/StaticDynamic.cs

44 lines
1.2 KiB
C#
Raw Normal View History

2019-08-08 21:12:11 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using System.Reflection;
namespace Toolbox.Library
{
public class StaticDynamic : DynamicObject
{
private Type _type;
public StaticDynamic(Type type) { _type = type; }
// Handle static properties
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
PropertyInfo prop = _type.GetProperty(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public);
if (prop == null)
{
result = null;
return false;
}
result = prop.GetValue(null, null);
return true;
}
// Handle static methods
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
MethodInfo method = _type.GetMethod(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public);
if (method == null)
{
result = null;
return false;
}
result = method.Invoke(null, args);
return true;
}
}
}