Using C# scripter to control host app.

To start: I have seen ObjectReference sample and added assembly reference for autocompletion. However, I must make it more simplified it for the "END USER".
In short: we need to add ability to control our host app via C# script. It is possible to do via object reference "scriptRun.GlobalItems". However, it still look difficult for the END USER.
1) I need to call functions in HostApp and control it from scripter: for example to call function DrawLine(X1, X2, Y1, Y2) (host machine) . Eventually, it will add line in canvas layout in WPF.
2) I would like to call it from script (user type script) without any additional "using", "class", "object.XXX" and so on.
* User still should have ability to write his own Classes etc or use #load function to load them.
3) I need to use object reference without preceding ScriptGlobalClass:
Instead of ScriptGlobalClass.RunButton.Content = "smth"
RunButton.Content="smth"
To summarize the script(written by end user) would look look like this:

DrawLine(0, 100, 0, 100);
RunButton.Content = "My custom text";
Hello,
If you'd like to omit ScriptGlobalClass in the calls like ScriptGlobalClass.RunButton.Text = "hello", then you would need to list ScriptGlobalClass in the using section like this:
using static ScriptGlobalClass;
Similarly, if you'd like to call a function like DrawLine of your own class (which must be static then for C# to be able to call it without object reference), you could use the same approach.
Say, you have the following class declared in ObjectReference namespace:
public static class MyClass
{
public static void DrawLine(int x, int y) { }
}
now you need to make MyClass available to the Script runner and Code editor code completion engine:
private void StartScript()
{
scriptRun.ScriptSource.FromScriptCode(edit.Text);
scriptRun.ScriptSource.WithDefaultReferences();
scriptRun.ScriptSource.References.Add(typeof(MyClass).Assembly.Location); // added line
....
}
private void RegisterScriptCodeForEditor()
{
edit.RegisterCode("global.cs", scriptRun.ScriptHost.GlobalCode);
edit.RegisterAssembly(typeof(MyClass).Assembly.Location); // added line
}
Now you can use DrawLine in the script like this:
using static ScriptGlobalClass;
using static ObjectReference.MyClass;
...
RunButton.Text = "Catch me if you can";
DrawLine(1, 2);
This leaves a question of how users can avoid adding these static using (or any other using) to using list. The only thing I can think of is to use the SnippetParser approach, where code being displayed in the editor is a subset of code being executed or parsed to provide code completion, etc.
Regards,
Andrew
Thank you very much. That helped a lot!
I managed to simplify code to:
using static ScriptGlobalClass;
using static ObjectReference.MyClass;

public class Program
{
public static void Main()
{
}
}
I have nothing against two using at the start of every script. However, I would like to simplify code to this:
using static ScriptGlobalClass;
using static ObjectReference.MyClass;

for (double nr = 0; nr < 10; nr +=1)
{
RunButton.Content = "new content" + nr;
DrawLine(0, 100 + nr * 10, 0, 300);
}
Sorry I made mistake in code:
Is it possible to achieve from this:
using static ScriptGlobalClass;
using static ObjectReference.MyClass;
public class Program
{
public static void Main()
{
for (double nr = 0; nr < 10; nr +=1)
{
RunButton.Content = "new content" + nr;
DrawLine(0, 100 + nr * 10, 0, 300);
}
}
}
To this?:
using static ScriptGlobalClass;
using static ObjectReference.MyClass;

for (double nr = 0; nr < 10; nr +=1)
{
RunButton.Content = "new content" + nr;
DrawLine(0, 100 + nr * 10, 0, 300);
}
Hi,
You will either need to use SnippetParser approach (where you display part of the code in the editor, but send to code completion engine and script execution engine the whole text
So instead of this code:
scriptRun.ScriptSource.FromScriptCode(edit.Text);
You would need to do something like this:
scriptRun.ScriptSource.FromScriptCode(string.Format(Snippet, edit.Text));
Alternatively, you can use Class-less scripts both for code editor and Scripter. These scripts can have global code (so no class/method declaration is declared).
To do so, you need to set ScriptRun.Language to CSharpScript and create csParser with CsSolution.DefaultScriptSolution parameter.
I've uploaded sample project showing how to achieve this here:
http://www.alternetsoft.com/forum/ObjectReference_07312020.zip
Regards,
Andrew