static string providerName = "cs";
static string sourceFileName = "test.cs";
/// <summary>
/// 클래스 생성 및 컴파일
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
CodeDomProvider provider = CodeDomProvider.CreateProvider(providerName);
CodeCompileUnit cu = new CodeCompileUnit();
cu = BuildHelloWorldGraph();
StreamWriter sourceFile = new StreamWriter(sourceFileName);
provider.GenerateCodeFromCompileUnit(cu, sourceFile, null);
sourceFile.Close();
CompilerParameters opt = new CompilerParameters(new string[] { "System.dll" });
opt.GenerateExecutable = true;
opt.OutputAssembly = "HelloWorld.exe";
opt.TreatWarningsAsErrors = true;
opt.IncludeDebugInformation = true;
opt.GenerateInMemory = true;
opt.CompilerOptions = "/doc:HelloWorldDoc.xml";
CompilerResults results;
results = provider.CompileAssemblyFromFile(opt, sourceFileName);
}
/// <summary>
/// 코드컴파일유닛 : 그래프를 생성하징
/// </summary>
/// <returns></returns>
public static CodeCompileUnit BuildHelloWorldGraph()
{
// Create a new CodeCompileUnit to contain the program graph.
CodeCompileUnit compileUnit = new CodeCompileUnit();
// Declare a new namespace called Samples.
CodeNamespace samples = new CodeNamespace("Samples");
// Add the new namespace to the compile unit.
compileUnit.Namespaces.Add(samples);
// Add the new namespace import for the System namespace.
samples.Imports.Add(new CodeNamespaceImport("System"));
#region 클래스 주석부분 선언하기
// Declare a new type called Class1.
CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1");
class1.Comments.Add(new CodeCommentStatement("<summary>", true));
class1.Comments.Add(new CodeCommentStatement("Create a Hello World application.", true));
class1.Comments.Add(new CodeCommentStatement(@"<seealso cref=" + '"' + "Class1.Main" + '"' + "/>", true));
class1.Comments.Add(new CodeCommentStatement("</summary>", true));
// Add the new type to the namespace type collection.
samples.Types.Add(class1);
// Declare a new code entry point method.
CodeEntryPointMethod start = new CodeEntryPointMethod();
start.Comments.Add(new CodeCommentStatement("<summary>", true));
start.Comments.Add(new CodeCommentStatement("Main method for HelloWorld application.", true));
start.Comments.Add(new CodeCommentStatement(@"<para>Add a new paragraph to the description.</para>", true));
start.Comments.Add(new CodeCommentStatement("</summary>", true));
#endregion
// Create a type reference for the System.Console class.
CodeTypeReferenceExpression csSystemConsoleType = new CodeTypeReferenceExpression("System.Console");
// CodeMethodInvokeExpression.CodeMethodInvokeExpression(CodeExpression targetObject, string methodName, params CodeExpression[] parameters)
// Build a Console.WriteLine statement.
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(csSystemConsoleType, "WriteLine",
new CodePrimitiveExpression("Hello World!"));
// Add the WriteLine call to the statement collection.
start.Statements.Add(cs1);
// Build another Console.WriteLine statement.
CodeMethodInvokeExpression cs2 = new CodeMethodInvokeExpression(csSystemConsoleType, "WriteLine",
new CodePrimitiveExpression("Press the ENTER key to continue."));
// Add the WriteLine call to the statement collection.
start.Statements.Add(cs2);
// Build a call to System.Console.ReadLine.
CodeMethodInvokeExpression csReadLine = new CodeMethodInvokeExpression(csSystemConsoleType, "ReadLine");
// Add the ReadLine statement.
start.Statements.Add(csReadLine);
// Add the code entry point method to the Members collection of the type.
class1.Members.Add(start);
return compileUnit;
} |