-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMainWindow.axaml.cs
66 lines (56 loc) · 3.18 KB
/
MainWindow.axaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using CSharpEditor;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System.IO;
using System.Reflection;
namespace CSharpEditorIPCDemoServer
{
public class MainWindow : Window
{
CSharpEditor.Editor Editor;
public MainWindow()
{
InitializeComponent();
// Initialise the debugger processs. The path passed to the constructor should be the path to the client debugger executable.
// If the client process dies unexpectedly, the debugger server will respawn it automatically.
InterprocessDebuggerServer server = new InterprocessDebuggerServer(@"../../../../CSharpEditorIPCDemoClient/bin/Debug/netcoreapp3.1/CSharpEditorIPCDemoClient.exe");
this.Opened += async (s, e) =>
{
// Initial source code
string sourceText = "";
using (Stream stream = this.GetType().Assembly.GetManifestResourceStream("CSharpEditorIPCDemoServer.HelloWorld.cs"))
using (StreamReader reader = new StreamReader(stream))
{
sourceText = reader.ReadToEnd();
}
// Minimal set of references for a console application - double check these with your target framework version - sometimes they change.
string systemRuntime = Path.Combine(Path.GetDirectoryName(typeof(object).Assembly.Location), "System.Runtime.dll");
CSharpEditor.CachedMetadataReference[] minimalReferences = new CSharpEditor.CachedMetadataReference[]
{
CSharpEditor.CachedMetadataReference.CreateFromFile(systemRuntime), // System.Runtime.dll
CSharpEditor.CachedMetadataReference.CreateFromFile(typeof(object).Assembly.Location), // System.Private.CoreLib.dll
CSharpEditor.CachedMetadataReference.CreateFromFile(typeof(System.Console).Assembly.Location) // System.Console.dll
};
Editor = await CSharpEditor.Editor.Create(sourceText, references: minimalReferences, compilationOptions: new CSharpCompilationOptions(OutputKind.ConsoleApplication));
Grid.SetRow(Editor, 1);
this.FindControl<Grid>("MainGrid").Children.Add(Editor);
};
this.FindControl<Button>("RunButton").Click += async (s, e) =>
{
// We use the SynchronousBreak and AsynchronousBreak from the debugger server, rather than the editor.
Assembly assembly = (await Editor.Compile(server.SynchronousBreak(Editor), server.AsynchronousBreak(Editor))).Assembly;
if (assembly != null)
{
// Note how the code is being executed on the UI thread.
assembly.EntryPoint.Invoke(null, new object[assembly.EntryPoint.GetParameters().Length]);
}
};
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}