コード例 #1
0
 /// <summary>
 /// </summary>
 /// <param name="functionName">
 /// </param>
 /// <param name="character">
 /// </param>
 public void CallMethod(string functionName, Character character)
 {
     foreach (Assembly assembly in this.multipleDllList)
     {
         foreach (KeyValuePair <string, Type> kv in this.scriptList)
         {
             if (kv.Key.Substring(kv.Key.IndexOf(":", StringComparison.Ordinal)) == ":" + functionName)
             {
                 IAOScript aoScript =
                     (IAOScript)
                     assembly.CreateInstance(
                         kv.Key.Substring(0, kv.Key.IndexOf(":", StringComparison.Ordinal)));
                 if (aoScript != null)
                 {
                     kv.Value.InvokeMember(
                         functionName,
                         BindingFlags.Default | BindingFlags.InvokeMethod,
                         null,
                         aoScript,
                         new object[] { character },
                         CultureInfo.InvariantCulture);
                 }
             }
         }
     }
 }
コード例 #2
0
        /// <summary>
        /// Loads all classes contained in our
        /// Assembly file that publically inherit
        /// our IAOScript class.
        /// Entry point for each script is public void Main(string[] args){}
        /// </summary>
        /// <param name="script">
        /// Our .NET dll or exe file.
        /// </param>
        private static void RunScript(Assembly script)
        {
            Contract.Requires(script != null);

            // Now that we have a compiled script, lets run them
            foreach (Type type in script.GetExportedTypes())
            {
                // returns all public types in the asembly
                foreach (Type iface in type.GetInterfaces())
                {
                    if (iface == typeof(IAOScript))
                    {
                        // yay, we found a script interface, lets create it and run it!
                        // Get the constructor for the current type
                        // you can also specify what creation parameter types you want to pass to it,
                        // so you could possibly pass in data it might need, or a class that it can use to query the host application
                        ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
                        if (constructor != null && constructor.IsPublic)
                        {
                            // lets be friendly and only do things legitimitely by only using valid constructors
                            // we specified that we wanted a constructor that doesn't take parameters, so don't pass parameters
                            IAOScript scriptObject = constructor.Invoke(null) as IAOScript;
                            if (scriptObject != null)
                            {
                                LogScriptAction(
                                    "Script",
                                    ConsoleColor.Green,
                                    scriptObject.GetType().Name + " Loaded.",
                                    ConsoleColor.Green);

                                // Lets run our script and display its results
                                scriptObject.Main(null);
                            }
                            else
                            {
                                // hmmm, for some reason it didn't create the object
                                // this shouldn't happen, as we have been doing checks all along, but we should
                                // inform the user something bad has happened, and possibly request them to send
                                // you the script so you can debug this problem
                                LogScriptAction("Error!", ConsoleColor.Red, "Script not loaded.", ConsoleColor.Red);
                            }
                        }
                        else
                        {
                            // and even more friendly and explain that there was no valid constructor
                            // found and thats why this script object wasn't run
                            LogScriptAction("Error!", ConsoleColor.Red, "No valid constructor found.", ConsoleColor.Red);
                        }
                    }
                }
            }
        }