예제 #1
0
        public void StartCSharpCompilation(object sender, EventArgs eventArgs)
        {
            if (TopComment.Visibility == Visibility.Visible)
            {
                TopComment.Visibility = Visibility.Hidden;
                TopComment.HostNode_PropertyChanged(null, null);
            }

            scriptingControl.CurrentFile.ScriptContent = scriptingControl.TextEditor.Text;

            // Define the Input for the Script
            try
            {
                mScriptCompiler = new Lazy <CSharpScriptCompiler>();


                //Compile and execute current script
                var result = mScriptCompiler.Value.Compile(scriptingControl.CurrentFile as CSharpScriptFile);


                if (result.GetType() == typeof(CompilerErrorCollection))
                {
                    var compilerErrors          = new StringBuilder();
                    var compilerErrorCollection = result as CompilerErrorCollection;
                    if (compilerErrorCollection == null)
                    {
                        throw new InvalidOperationException(
                                  "Unable to compile scripts: " +
                                  compilerErrors);
                    }
                    foreach (var actError in compilerErrorCollection)
                    {
                        compilerErrors.Append("" + actError + Environment.NewLine);
                    }
                    TopComment.Text       = compilerErrors.ToString();
                    TopComment.Visibility = Visibility.Visible;
                    TopComment.HostNode_PropertyChanged(null, null);
                    throw new InvalidOperationException(
                              "Unable to compile scripts: " +
                              compilerErrors);
                }


                var parameter = mScriptCompiler.Value.ScriptMethod.GetParameters();

                var counter = 0;
                foreach (var para in parameter)
                {
                    if (counter < InputPorts.Count)
                    {
                        if (para.ParameterType.IsGenericType)
                        {
                            InputPorts[counter].MultipleConnectionsAllowed = true;
                            InputPorts[counter].DataType = para.ParameterType.GetGenericArguments()[0];
                        }
                        else
                        {
                            InputPorts[counter].DataType = para.ParameterType;
                        }

                        InputPorts[counter].Name = para.Name;
                    }
                    else
                    {
                        if (para.ParameterType.IsGenericType)
                        {
                            AddInputPortToNode(para.Name, para.ParameterType.GetGenericArguments()[0], true);
                        }
                        else
                        {
                            AddInputPortToNode(para.Name, para.ParameterType);
                        }
                    }


                    counter++;
                }

                while (counter < InputPorts.Count())
                {
                    RemoveInputPortFromNode(InputPorts.Last());
                }

                Calculate();

                TopComment.Text       = "";
                TopComment.Visibility = Visibility.Collapsed;
            }
            catch (Exception e)
            {
                TopComment.Text       = e.ToString();
                TopComment.Visibility = Visibility.Visible;
                TopComment.HostNode_PropertyChanged(null, null);

                Console.WriteLine(e.Message);
            }
        }