CodeDom在哪里保存生成的.cs文件?我们可以将此位置更改为其他文件夹

时间:2016-07-21 08:23:48

标签: c# asp.net .net code-generation codedom

我已成功运行codedam示例项目。 输出文件位于bin调试文件夹中。

生成一个c#类文件。 我需要在 D:/ Temp文件夹中生成此c#类文件 我不生成任何可执行文件 out put是C#.cs文件。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.CodeDom.Compiler;
using System.CodeDom;
using Microsoft.CSharp;
using System.Reflection;


namespace codeDamnSample
{


    public class CCodeGenerator
    {
        CodeNamespace mynamespace;
        CodeTypeDeclaration myclass;
        CodeCompileUnit myassembly;

        public void CreateNamespace()
        {
            mynamespace = new CodeNamespace("mynamespace");
        }
        public void CreateImports()
        {
            mynamespace.Imports.Add(new CodeNamespaceImport("System"));

        }
        public void CreateClass()
        {
            myclass = new CodeTypeDeclaration();
            myclass.Name = "SampleTest:TestBase";
            myclass.IsClass = true;
            myclass.Attributes = MemberAttributes.Public;
            mynamespace.Types.Add(myclass);
        }
        public void CreateMethod()
        {
            CodeMemberMethod mymethod = new CodeMemberMethod();
            mymethod.Name = testMethod;
            CodeTypeReference ctr = new CodeTypeReference();
            //Assign the return type to the method.
            mymethod.ReturnType = ctr;
            //Provide definition to the method (returns the sum of two //numbers)
            CodeSnippetExpression snippet1 = new CodeSnippetExpression("AutomationBase obj = new AutomationBase()");
            //return the value
            CodeSnippetExpression snippet2 = new CodeSnippetExpression("obj.Execute(testCases[0])");
            //Convert the snippets into Expression statements.
            CodeExpressionStatement stmt1 = new CodeExpressionStatement(snippet1);
            CodeExpressionStatement stmt2 = new CodeExpressionStatement(snippet2);
            //Add the expression statements to the method.
            mymethod.Statements.Add(stmt1);
            mymethod.Statements.Add(stmt2);
            //Provide the access modifier for the method.
            mymethod.Attributes = MemberAttributes.Public;
            CodeAttributeDeclaration codeAttrDecl = new CodeAttributeDeclaration("Test");

            mymethod.CustomAttributes.Add(codeAttrDecl);
            //Finally add the method to the class.
            myclass.Members.Add(mymethod);
        }
        public void SaveAssembly()
        {
            myassembly = new CodeCompileUnit();
            myassembly.Namespaces.Add(mynamespace);
            CompilerParameters comparam = new CompilerParameters(new string[] { "mscorlib.dll" });
            comparam.GenerateInMemory = false;
            comparam.GenerateExecutable = false;
            comparam.MainClass = "mynamespace.CMyclass";
            Microsoft.CSharp.CSharpCodeProvider ccp = new Microsoft.CSharp.CSharpCodeProvider();

            String sourceFile;
            ccp.
            if (ccp.FileExtension[0] == '.')
            {
                sourceFile = "SampleTest" + ccp.FileExtension;
            }
            else
            {
                sourceFile = "SampleTest." + ccp.FileExtension;
            }
            var tw1 = new IndentedTextWriter(new StreamWriter(sourceFile, false), "    ");
            ccp.GenerateCodeFromCompileUnit(myassembly, tw1, new CodeGeneratorOptions());
            tw1.Close();

            ccp.CompileAssemblyFromDom(comparam, myassembly);


        }
        public static void generateCod()
        {  
            CCodeGenerator cg = new CCodeGenerator();
            cg.CreateNamespace();
            cg.CreateImports();
            cg.CreateClass();
            cg.CreateMember();
            cg.CreateProperty();
           cg.CreateMethod();

          //  cg.CreateEntryPoint();
            cg.SaveAssembly();
           // Console.ReadLine();
        }


    }


}

1 个答案:

答案 0 :(得分:0)

它应该将文件保存在当前工作目录中,通常是正在运行的项目中的bin/Debug文件夹。

相关问题