/// <summary> /// Gets all class implementing an interface /// </summary> /// <param name="type">Type of the interface or null for evey interfaces</param> /// <returns>List of classes</returns> public List <string> GetImplementedInterfaces(Type type) { List <string> list = new List <string>(); if (!IsCompiled) { Compile(); } if (CompiledAssembly == null) { return(list); } // Loop through types looking for one that implements the given interface foreach (Type t in CompiledAssembly.GetTypes()) { foreach (Type ty in t.GetInterfaces()) { if (ty == type || type == null) { list.Add(t.Name); } } } list.Sort(); return(list); }
/// <summary> /// Returns all methods /// </summary> /// <returns>Returns a list of method's name</returns> public List <string> GetMethods() { List <string> list = new List <string>(); if (!IsCompiled) { Compile(); } if (CompiledAssembly == null) { return(list); } // Loop through types looking for one that implements the given interface foreach (Type t in CompiledAssembly.GetTypes()) { foreach (MethodInfo m in t.GetMethods()) { list.Add(m.Name); } } list.Sort(); return(list); }
/// <summary> /// Gets a list of specific methods /// </summary> /// <param name="types">An array of type's parameters</param> /// <param name="ret">Retrun value</param> /// <returns></returns> public List <string> GetMethods(Type[] types, Type ret) { List <string> list = new List <string>(); if (!IsCompiled) { Compile(); } if (CompiledAssembly == null) { return(list); } // Loop through types looking for one that implements the given interface foreach (Type t in CompiledAssembly.GetTypes()) { foreach (MethodInfo m in t.GetMethods()) { // return value if (m.ReturnType != ret) { continue; } // Parameters bool ok = false; int id = 0; foreach (ParameterInfo pi in m.GetParameters()) { // Index overrun if (id >= types.Length) { ok = false; break; } // Parameters differents if (pi.ParameterType != types[id]) { ok = false; break; } ok = true; // Next id++; } if (ok && id == types.Length) { list.Add(m.Name); } } } return(list); }
/// <summary> /// Invoke a method in the script /// </summary> /// <param name="method">Method's name</param> /// <param name="args">Arguments</param> /// <returns>true if args or false</returns> // http://msdn.microsoft.com/en-us/library/3y322t50%28v=VS.90%29.aspx => Reflection.Emmit() public bool Invoke(string method, params object[] args) { if (!IsCompiled) { Trace.WriteLine("Invoking method \"" + method + "\" on uncompiled script \"" + Name + "\""); return(false); } try { Type[] type = CompiledAssembly.GetTypes(); MethodInfo[] mi = type[0].GetMethods(); object instance = Activator.CreateInstance(type[0]); object result = type[0].InvokeMember(method, BindingFlags.Default | BindingFlags.InvokeMethod, null, instance, args); } catch (Exception e) { Trace.WriteLine("Exception invoking \"" + method + "\" on uncompiled script \"" + Name + "\" : " + e.Message); } return(true); }