Runtime компиляция в оконное приложение - C#
Формулировка задачи:
В общем есть такой код, он компилирует вторую прогу runtime, но она получается консольная, а может ли она быть оконной, не консольной?
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Parser1
{
static class Program
{
/// <summary>
/// Главная точка входа для приложения.
/// </summary>
[STAThread]
static void Main()
{
Dictionary<string, string> opt = new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } };
CSharpCodeProvider pr = new CSharpCodeProvider(opt);
CompilerParameters p = new CompilerParameters();
p.ReferencedAssemblies.Add("System.Dll");
p.OutputAssembly = "Asd.exe";
p.GenerateExecutable = true;
string s = @"
using System;
using System.Diagnostics;
namespace Parser1
{
static class Program
{
/// <summary>
/// Главная точка входа для приложения.
/// </summary>
[STAThread]
static void Main()
{
Process.Start(""123.xls"");
}
}
}
";
CompilerResults r = pr.CompileAssemblyFromSource(p, s);
foreach (CompilerError err in r.Errors)
{
MessageBox.Show(err.ErrorText);
}
}
}
}Решение задачи: «Runtime компиляция в оконное приложение»
textual
Листинг программы
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
namespace ConsoleAppTest
{
class MainClass
{
public static void Main(string[] args) {
var opt = new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } };
CSharpCodeProvider pr = new CSharpCodeProvider(opt);
CompilerParameters p = new CompilerParameters();
p.CompilerOptions = "/target:winexe";
p.ReferencedAssemblies.Add("System.Windows.Forms.dll");
// может понадобиться
// p.ReferencedAssemblies.Add("System.Drawing.dll");
p.OutputAssembly = "Asd.exe";
p.GenerateExecutable = true;
string s = @"
using System;
using System.Windows.Forms;
namespace Parser1
{
static class Program
{
/// <summary>
/// Главная точка входа для приложения.
/// </summary>
[STAThread]
static void Main()
{
Form form = new Form();
Button btn = new Button();
btn.Text = " + "\"Hello\";" + @"
btn.Parent = form;
Application.Run(form);
}
}
}
";
CompilerResults r = pr.CompileAssemblyFromSource(p, s);
foreach (CompilerError err in r.Errors)
{
MessageBox.Show(err.ErrorText);
}
}
}
}