如何执行使用codeDom生成的.cs文件

时间:2010-07-22 04:49:14

标签: c#-3.0

这是我的源代码.....

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;
using Microsoft.CSharp;
using Microsoft.VisualBasic;
namespace CodeDOM     
{
 class Program
 {     
    private string m_strFileName;   
    private string m_Suffix = ".cs";
  public Program(string FileName)
  {
      m_strFileName = FileName;
  }
  public void CreateCodeDom()
  {
      Stream codeFile = File.Open("c:\\" + m_strFileName + m_Suffix, FileMode.Create);
      StreamWriter sw = new StreamWriter(codeFile);
      CSharpCodeProvider cscp = new CSharpCodeProvider();                 
      ICodeGenerator codeGenerator = cscp.CreateGenerator(sw);
      CodeGeneratorOptions cgo = new CodeGeneratorOptions();
      CodeSnippetCompileUnit cscu = new CodeSnippetCompileUnit("imports System");
      codeGenerator.GenerateCodeFromCompileUnit(cscu, sw, cgo);
      CodeNamespace cnsCodeDom = new CodeNamespace("SampleNamespace");
      CodeTypeDeclaration clsDecl = new CodeTypeDeclaration();
      clsDecl.Name = "Sample";
      clsDecl.IsClass = true;
      clsDecl.TypeAttributes = TypeAttributes.Public;
      cnsCodeDom.Types.Add(clsDecl);
      CodeConstructor clsConstructor = new CodeConstructor();
      clsConstructor.Attributes = MemberAttributes.Public;
      clsDecl.Members.Add(clsConstructor);
      CodeMemberField clsMember = new CodeMemberField();
      clsMember.Name = "myname";
      clsMember.Attributes = MemberAttributes.Private;
      clsMember.Type = new CodeTypeReference("System.String");
      clsDecl.Members.Add(clsMember);
      CodeMemberProperty property = new CodeMemberProperty();
      property.Name = "MyName";
      property.Type = new CodeTypeReference("System.String");
      property.Attributes = MemberAttributes.Public;          
      property.HasGet = true;
      property.GetStatements.Add(new CodeSnippetExpression("return this.myname"));
      property.HasSet = true;
      property.SetStatements.Add(new CodeSnippetExpression("this.myname=value"));
      //property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "this.myname")));
      //property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "this.myname"), new CodePropertySetValueReferenceExpression()));
      clsDecl.Members.Add(property);
      CodeMemberMethod mtd = new CodeMemberMethod();
      mtd.Name = "Print";
      mtd.ReturnType = null;
      mtd.Attributes = MemberAttributes.Public;
      mtd.Statements.Add(new CodeSnippetStatement("Console.WriteLine(myname)"));
      clsDecl.Members.Add(mtd);
      codeGenerator.GenerateCodeFromNamespace(cnsCodeDom, sw, cgo);
      sw.Close();
      codeFile.Close();
  }        
   static void Main(string[] args)
   {
       Program m_obj = new Program("Sample");
       m_obj.CreateCodeDom();
       Console.ReadLine();
   }
}

}

从这里我得到了sample.cs文件。现在我想执行那个sample.cs文件。请问你怎么做?

1 个答案:

答案 0 :(得分:0)

如果它是一个应用程序,只需在一个源文件中执行:

csc sample.cs && sample.exe