using System; using System.Reflection; namespace FolderSelect { /// /// This class is from the Front-End for Dosbox and is used to present a 'vista' dialog box to select folders. /// Being able to use a vista style dialog box to select folders is much better then using the shell folder browser. /// http://code.google.com/p/fed/ /// /// Example: /// var r = new Reflector("System.Windows.Forms"); /// public class Reflector { #region variables string m_ns; Assembly m_asmb; #endregion #region Constructors /// /// Constructor /// /// The namespace containing types to be used public Reflector(string ns) : this(ns, ns) { } /// /// Constructor /// /// A specific assembly name (used if the assembly name does not tie exactly with the namespace) /// The namespace containing types to be used public Reflector(string an, string ns) { m_ns = ns; m_asmb = null; foreach (AssemblyName aN in Assembly.GetExecutingAssembly().GetReferencedAssemblies()) { if (aN.FullName.StartsWith(an)) { m_asmb = Assembly.Load(aN); break; } } } #endregion #region Methods /// /// Return a Type instance for a type 'typeName' /// /// The name of the type /// A type instance public Type GetType(string typeName) { Type type = null; string[] names = typeName.Split('.'); if (names.Length > 0) type = m_asmb.GetType(m_ns + "." + names[0]); for (int i = 1; i < names.Length; ++i) { type = type.GetNestedType(names[i], BindingFlags.NonPublic); } return type; } /// /// Create a new object of a named type passing along any params /// /// The name of the type to create /// /// An instantiated type public object New(string name, params object[] parameters) { Type type = GetType(name); ConstructorInfo[] ctorInfos = type.GetConstructors(); foreach (ConstructorInfo ci in ctorInfos) { try { return ci.Invoke(parameters); } catch { } } return null; } /// /// Calls method 'func' on object 'obj' passing parameters 'parameters' /// /// The object on which to excute function 'func' /// The function to execute /// The parameters to pass to function 'func' /// The result of the function invocation public object Call(object obj, string func, params object[] parameters) { return Call2(obj, func, parameters); } /// /// Calls method 'func' on object 'obj' passing parameters 'parameters' /// /// The object on which to excute function 'func' /// The function to execute /// The parameters to pass to function 'func' /// The result of the function invocation public object Call2(object obj, string func, object[] parameters) { return CallAs2(obj.GetType(), obj, func, parameters); } /// /// Calls method 'func' on object 'obj' which is of type 'type' passing parameters 'parameters' /// /// The type of 'obj' /// The object on which to excute function 'func' /// The function to execute /// The parameters to pass to function 'func' /// The result of the function invocation public object CallAs(Type type, object obj, string func, params object[] parameters) { return CallAs2(type, obj, func, parameters); } /// /// Calls method 'func' on object 'obj' which is of type 'type' passing parameters 'parameters' /// /// The type of 'obj' /// The object on which to excute function 'func' /// The function to execute /// The parameters to pass to function 'func' /// The result of the function invocation public object CallAs2(Type type, object obj, string func, object[] parameters) { MethodInfo methInfo = type.GetMethod(func, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); return methInfo.Invoke(obj, parameters); } /// /// Returns the value of property 'prop' of object 'obj' /// /// The object containing 'prop' /// The property name /// The property value public object Get(object obj, string prop) { return GetAs(obj.GetType(), obj, prop); } /// /// Returns the value of property 'prop' of object 'obj' which has type 'type' /// /// The type of 'obj' /// The object containing 'prop' /// The property name /// The property value public object GetAs(Type type, object obj, string prop) { PropertyInfo propInfo = type.GetProperty(prop, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); return propInfo.GetValue(obj, null); } /// /// Returns an enum value /// /// The name of enum type /// The name of the value /// The enum value public object GetEnum(string typeName, string name) { Type type = GetType(typeName); FieldInfo fieldInfo = type.GetField(name); return fieldInfo.GetValue(null); } #endregion } }