using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; namespace BizHawk.Common.ReflectionExtensions { /// /// Reflection based helper methods /// public static class ReflectionUtil { public static string Description(this Type type) { var descriptions = (DescriptionAttribute[]) type.GetCustomAttributes(typeof(DescriptionAttribute), false); if (descriptions.Length == 0) { return string.Empty; } return descriptions[0].Description; } /// /// Takes an object and determines if it has methodName as a public method /// /// Returns whether or not the obj both contains the method name and the method is public public static bool HasExposedMethod(this object obj, string methodName) { var method = obj.GetType().GetMethod(methodName); if (method != null) { return method.IsPublic; } return false; } /// /// Takes an object and invokes the method /// The method must exist and be public /// /// 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 /// public static object InvokeMethod(this object obj, string methodName, object[] args) { var method = obj.GetType().GetMethod(methodName); if (method != null && method.IsPublic) { return method.Invoke(obj, args); } return null; } public static bool HasPublicProperty(this object obj, string propertyName) { var property = obj.GetType().GetProperty(propertyName); if (property != null) { return property.CanRead; } return false; } public static object GetPropertyValue(this object obj, string propertyName) { var property = obj.GetType().GetProperty(propertyName); if (property != null && property.CanRead) { return property.GetValue(obj, null); } return null; } } }