1
0
mirror of synced 2024-12-04 20:08:00 +01:00
Switch-Toolbox/Switch_Toolbox_Library/StaticDynamic.cs
2019-08-08 17:12:11 -04:00

44 lines
1.2 KiB
C#

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;
}
}
}