namespace System.Reflection { /// Extensions related to System.Reflection internal static class ReflectionHelper { /// Loads a type from a named assembly. /// Name of the type. /// The name or path of the file that contains the manifest of the assembly. /// The reference, or null if type or assembly not found. public static Type LoadType(string typeName, string asmRef) { Type ret = null; if (!TryGetType(Assembly.GetCallingAssembly(), typeName, ref ret)) if (!TryGetType(asmRef, typeName, ref ret)) if (!TryGetType(Assembly.GetExecutingAssembly(), typeName, ref ret)) TryGetType(Assembly.GetEntryAssembly(), typeName, ref ret); return ret; } /// Tries the retrieve a reference from an assembly. /// Name of the type. /// The assembly reference name from which to load the type. /// The reference, if found. /// true if the type was found in the assembly; otherwise, false. private static bool TryGetType(string asmRef, string typeName, ref Type type) { try { if (System.IO.File.Exists(asmRef)) return TryGetType(Assembly.LoadFrom(asmRef), typeName, ref type); } catch { } return false; } /// Tries the retrieve a reference from an assembly. /// Name of the type. /// The assembly from which to load the type. /// The reference, if found. /// true if the type was found in the assembly; otherwise, false. private static bool TryGetType(Assembly asm, string typeName, ref Type type) { if (asm != null) { try { type = asm.GetType(typeName, false, false); return (type != null); } catch { } } return false; } /// Invokes a named method on a created instance of a type with parameters. /// The expected type of the method's return value. /// The type to be instantiated and then used to invoke the method. This method assumes the type has a default public constructor. /// Name of the method. /// The arguments to provide to the method invocation. /// The value returned from the method. public static T InvokeMethod(Type type, string methodName, params object[] args) { object o = Activator.CreateInstance(type); return InvokeMethod(o, methodName, args); } /// Invokes a named method on a created instance of a type with parameters. /// The expected type of the method's return value. /// The type to be instantiated and then used to invoke the method. /// The arguments to supply to the constructor. /// Name of the method. /// The arguments to provide to the method invocation. /// The value returned from the method. public static T InvokeMethod(Type type, object[] instArgs, string methodName, params object[] args) { object o = Activator.CreateInstance(type, instArgs); return InvokeMethod(o, methodName, args); } /// Invokes a named method on an object with parameters and no return value. /// The object on which to invoke the method. /// Name of the method. /// The arguments to provide to the method invocation. public static T InvokeMethod(object obj, string methodName, params object[] args) { Type[] argTypes = (args == null || args.Length == 0) ? Type.EmptyTypes : Array.ConvertAll(args, delegate(object o) { return o == null ? typeof(object) : o.GetType(); }); return InvokeMethod(obj, methodName, argTypes, args); } /// Invokes a named method on an object with parameters and no return value. /// The expected type of the method's return value. /// The object on which to invoke the method. /// Name of the method. /// The types of the . /// The arguments to provide to the method invocation. /// The value returned from the method. public static T InvokeMethod(object obj, string methodName, Type[] argTypes, object[] args) { MethodInfo mi = obj?.GetType().GetMethod(methodName, argTypes); if (mi != null) return (T)Convert.ChangeType(mi.Invoke(obj, args), typeof(T)); return default(T); } /// Gets a named property value from an object. /// The expected type of the property to be returned. /// The object from which to retrieve the property. /// Name of the property. /// The default value to return in the instance that the property is not found. /// The property value, if found, or the if not. public static T GetProperty(object obj, string propName, T defaultValue = default(T)) { if (obj != null) { try { return (T)Convert.ChangeType(obj.GetType().InvokeMember(propName, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, obj, null, null), typeof(T)); } catch { } } return defaultValue; } /// Sets a named property on an object. /// The type of the property to be set. /// The object on which to set the property. /// Name of the property. /// The property value to set on the object. public static void SetProperty(object obj, string propName, T value) { try { obj?.GetType().InvokeMember(propName, BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, obj, new object[] { value }, null); } catch { } } } }