Пример #1
0
        /// <summary>
        /// User has specified a startup script - execute it.
        /// </summary>
        private void ProcessStartupScript(string fileName)
        {
            StreamReader reader = new StreamReader(fileName);
            string       code   = reader.ReadToEnd();

            reader.Close();
            Assembly compiledAssembly = ReflectionUtilities.CompileTextToAssembly(code, null);

            // Get the script 'Type' from the compiled assembly.
            Type scriptType = compiledAssembly.GetType("Script");

            if (scriptType == null)
            {
                throw new Exception("Cannot find a public class called 'Script'");
            }

            // Look for a method called Execute
            MethodInfo executeMethod = scriptType.GetMethod("Execute");

            if (executeMethod == null)
            {
                throw new Exception("Cannot find a method Script.Execute");
            }

            // Create a new script model.
            object script = compiledAssembly.CreateInstance("Script");

            // Call Execute on our newly created script instance.
            object[] arguments = new object[] { Presenter1 };
            executeMethod.Invoke(script, arguments);
        }
Пример #2
0
        /// <summary>Execute the specified script, returning any error messages or NULL if all OK.</summary>
        public string ProcessStartupScript(string code)
        {
            Assembly compiledAssembly = ReflectionUtilities.CompileTextToAssembly(code, null);

            // Get the script 'Type' from the compiled assembly.
            Type scriptType = compiledAssembly.GetType("Script");

            if (scriptType == null)
            {
                throw new Exception("Cannot find a public class called 'Script'");
            }

            // Look for a method called Execute
            MethodInfo executeMethod = scriptType.GetMethod("Execute");

            if (executeMethod == null)
            {
                throw new Exception("Cannot find a method Script.Execute");
            }

            // Create a new script model.
            object script = compiledAssembly.CreateInstance("Script");

            // Call Execute on our newly created script instance.
            object[] arguments = new object[] { this };
            executeMethod.Invoke(script, arguments);
            return(null);
        }
Пример #3
0
        /// <summary>Rebuild the script model and return error message if script cannot be compiled.</summary>
        /// <exception cref="ApsimXException">
        /// Cannot find a public class called 'Script'
        /// </exception>
        public void RebuildScriptModel(List <Exception> errors)
        {
            if (HasLoaded)
            {
                // Capture the current values of all parameters.
                EnsureParametersAreCurrent();

                if (_Code != CompiledCode)
                {
                    // Compile the code.
                    Assembly compiledAssembly = null;

                    if (assemblyName != null)
                    {
                        foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                        {
                            if (assembly.FullName == assemblyName)
                            {
                                compiledAssembly = assembly;
                                break;
                            }
                        }
                    }

                    // When running a simulation, we don't want to waste time by re-compiling
                    // the script unnecessarily. But we need to be careful to avoid two sorts
                    // of problems: (1) failing to recompile when the script has been changed
                    // and (2) attempting to use a previously compiled assembly when the script
                    // has been modified and will now not compile correctly.
                    if (compiledAssembly == null || CompiledCode != null || lastCompileFailed)
                    {
                        try
                        {
                            compiledAssembly = ReflectionUtilities.CompileTextToAssembly(Code, GetAssemblyFileName());
                            if (compiledAssembly.Location != assemblyPath)
                            {
                                if (!string.IsNullOrEmpty(assemblyPath))
                                {
                                    File.Delete(assemblyPath);
                                    File.Delete(Path.ChangeExtension(assemblyPath, ".cs"));
                                    File.Delete(Path.ChangeExtension(assemblyPath, ".pdb"));
                                }
                                assemblyPath = compiledAssembly.Location;
                            }
                            // Get the script 'Type' from the compiled assembly.
                            if (compiledAssembly.GetType("Models.Script") == null)
                            {
                                throw new ApsimXException(this, "Cannot find a public class called 'Script'");
                            }

                            assemblyName      = compiledAssembly.FullName;
                            CompiledCode      = _Code;
                            lastCompileFailed = false;

                            // Create a new script model.
                            Script          = compiledAssembly.CreateInstance("Models.Script") as Model;
                            Script.Children = new System.Collections.Generic.List <Model>();
                            Script.Name     = "Script";
                            Script.IsHidden = true;
                            XmlElement parameters;

                            XmlDocument doc = new XmlDocument();
                            doc.LoadXml(elementsAsXml);
                            parameters = doc.DocumentElement;

                            SetParametersInObject(Script, parameters);

                            // Add the new script model to our models collection.
                            Children.Clear();
                            Children.Add(Script);
                            Script.Parent = this;
                        }
                        catch (Exception err)
                        {
                            lastCompileFailed = true;
                            errors.Add(new Exception("Unable to compile \"" + Name + "\"", err));
                        }
                    }
                }
            }
        }
Пример #4
0
        /// <summary>Rebuild the script model and return error message if script cannot be compiled.</summary>
        /// <exception cref="ApsimXException">
        /// Cannot find a public class called 'Script'
        /// </exception>
        public void RebuildScriptModel()
        {
            if (HasDeserialised)
            {
                // Capture the current values of all parameters.
                EnsureParametersAreCurrent();

                if (NeedToCompileCode())
                {
                    // If a script model already exists, then get rid of it.
                    if (Script != null)
                    {
                        Children.Remove(Script);
                        Script = null;
                    }

                    // Compile the code.
                    Assembly compiledAssembly = null;

                    if (assemblyName != null)
                    {
                        foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                        {
                            if (assembly.FullName == assemblyName)
                            {
                                compiledAssembly = assembly;
                                break;
                            }
                        }
                    }

                    // When running a simulation, we don't want to waste time by re-compiling
                    // the script unnecessarily. But we need to be careful to avoid two sorts
                    // of problems: (1) failing to recompile when the script has been changed
                    // and (2) attempting to use a previously compiled assembly when the script
                    // has been modified and will now not compile correctly.
                    if (compiledAssembly == null || CompiledCode != null || lastCompileFailed)
                    {
                        try
                        {
                            compiledAssembly = ReflectionUtilities.CompileTextToAssembly(Code, null);
                            // Get the script 'Type' from the compiled assembly.
                            if (compiledAssembly.GetType("Models.Script") == null)
                            {
                                throw new ApsimXException(this, "Cannot find a public class called 'Script'");
                            }
                        }
                        catch (Exception err)
                        {
                            lastCompileFailed = true;
                            throw new ApsimXException(this, err.Message);
                        }
                    }

                    assemblyName      = compiledAssembly.FullName;
                    CompiledCode      = _Code;
                    lastCompileFailed = false;

                    // Create a new script model.
                    Script          = compiledAssembly.CreateInstance("Models.Script") as Model;
                    Script.Children = new System.Collections.Generic.List <Model>();
                    Script.Name     = "Script";
                    Script.IsHidden = true;
                    XmlElement parameters;

                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(elementsAsXml);
                    parameters = doc.DocumentElement;

                    SetParametersInObject(Script, parameters);

                    // Add the new script model to our models collection.
                    Children.Add(Script);
                    Script.Parent = this;
                }
            }
        }