Skip to content

Instantly share code, notes, and snippets.

@svick
Last active January 8, 2023 05:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svick/7c29e29a9db532af0c619e6f3e32e814 to your computer and use it in GitHub Desktop.
Save svick/7c29e29a9db532af0c619e6f3e32e814 to your computer and use it in GitHub Desktop.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.HostModel" Version="3.1.16" />
</ItemGroup>
</Project>
using System;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.NET.HostModel.AppHost;
Directory.CreateDirectory(@"C:\code\tmp\HelloWorld");
var compilation = CSharpCompilation.Create(null)
.AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(@"System.Console.WriteLine(""Hello, World!"");"))
.AddReferences(Directory.EnumerateFiles(@"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0\ref\net5.0", "*.dll")
.Select(f => MetadataReference.CreateFromFile(f)));
var result = compilation.Emit(@"C:\code\tmp\HelloWorld\HelloWorld.dll");
foreach (var diagnostic in result.Diagnostics)
{
Console.WriteLine(diagnostic);
}
HostWriter.CreateAppHost(
appHostSourceFilePath: @"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Host.win-x64\5.0.11\runtimes\win-x64\native\apphost.exe",
appHostDestinationFilePath: @"C:\code\tmp\HelloWorld\HelloWorld.exe",
appBinaryFilePath: @"C:\code\tmp\HelloWorld\HelloWorld.dll");
File.WriteAllText(@"C:\code\tmp\HelloWorld\HelloWorld.deps.json", @"{
""runtimeTarget"": {
""name"": "".NETCoreApp,Version=v5.0"",
""signature"": """"
},
""compilationOptions"": {},
""targets"": {
"".NETCoreApp,Version=v5.0"": {
""HelloWorld/1.0.0"": {
""runtime"": {
""HelloWorld.dll"": {}
}
}
}
},
""libraries"": {
""HelloWorld/1.0.0"": {
""type"": ""project"",
""serviceable"": false,
""sha512"": """"
}
}
}");
File.WriteAllText(@"C:\code\tmp\HelloWorld\HelloWorld.runtimeconfig.json", @"{
""runtimeOptions"": {
""tfm"": ""net5.0"",
""framework"": {
""name"": ""Microsoft.NETCore.App"",
""version"": ""5.0.0""
}
}
}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment