BizHawk/BizHawk.Common/ReflectionUtil.cs

50 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace BizHawk.Common
{
/// <summary>
/// Reflection based helper methods
/// </summary>
public static class ReflectionUtil
{
/// <summary>
/// Takes an object and determines if it has methodName as a public method
/// </summary>
/// <returns>Returns whether or not the obj both contains the method name and the method is public</returns>
public static bool HasExposedMethod(object obj, string methodName)
{
var method = obj.GetType().GetMethod(methodName);
if (method != null)
{
return method.IsPublic;
}
return false;
}
/// <summary>
/// Takes an object and invokes the method
/// The method must exist and be public
/// </summary>
/// <returns>The return value of the method, as an object.
/// If the method returns void, the return value is null
/// If the method does not exist or is not public, it returns null
/// </returns>
public static object InvokeMethod(object obj, string methodName, object[] args)
{
var method = obj.GetType().GetMethod(methodName);
if (method != null && method.IsPublic)
{
return method.Invoke(obj, args);
}
return null;
}
}
}