/
EugenyCh7
/
CodeDOMTest
Обзор
Документация
Войти
/
EugenyCh7
/
CodeDOMTest
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
RoslynTest/Program.cs
64 строки
2 KB
eugenych
runtimePath fix
04 авг 2023, 17:00
04 авг 2023, 17:00
348cc75
Код
Авторство
О чём код?
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Text; using System.Text; namespace RoslynTest { internal class Program { private static readonly IEnumerable<string> DefaultNamespaces = new[] { "System" }; private static string runtimePath = typeof(object).Assembly.Location; private static readonly IEnumerable<MetadataReference> DefaultReferences = new[] { MetadataReference.CreateFromFile(string.Format(runtimePath, "System")) }; private static readonly CSharpCompilationOptions DefaultCompilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary) .WithOverflowChecks(true).WithOptimizationLevel(OptimizationLevel.Release) .WithUsings(DefaultNamespaces); public static SyntaxTree Parse(string text, string filename = "", CSharpParseOptions options = null) { var stringText = SourceText.From(text, Encoding.UTF8); return SyntaxFactory.ParseSyntaxTree(stringText, options, filename); } private static void Main(string[] args) { var fileToCompile = @"C:\Users\eugen\source\repos\CodeDOMTest\CodeDOMTest\Primes.cs"; var source = File.ReadAllText(fileToCompile); var parsedSyntaxTree = Parse(source, "", CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp11)); var compilation = CSharpCompilation.Create("Test.dll", new SyntaxTree[] { parsedSyntaxTree }, DefaultReferences, DefaultCompilationOptions); try { var result = compilation.Emit(@"D:\Downloads\Test.dll"); Console.WriteLine(result.Success ? "Success!!!" : "Failed"); if (!result.Success) { foreach (Diagnostic item in result.Diagnostics) { Console.WriteLine(item); } } } catch (Exception ex) { Console.WriteLine(ex); } Console.Read(); } } }