CodeDom 在哪里保存生成的 .cs 文件?我们可以将此位置更改为其他文件夹吗
Where does CodeDom save generated .cs files? Can we change this location to a Different Folder
我已成功 运行 codedam 示例项目。
输出文件在 bin debug 文件夹中。
正在生成一个 c# class 文件。
我需要在 D:/Temp 文件夹 中生成此 c# class 文件
我没有生成任何可执行文件
输出是 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();
}
}
}
它应该将文件保存在当前工作目录中,通常是 运行 项目中的 bin/Debug
文件夹。
我已成功 运行 codedam 示例项目。 输出文件在 bin debug 文件夹中。
正在生成一个 c# class 文件。 我需要在 D:/Temp 文件夹 中生成此 c# class 文件 我没有生成任何可执行文件 输出是 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();
}
}
}
它应该将文件保存在当前工作目录中,通常是 运行 项目中的 bin/Debug
文件夹。