How to create CodeCompileUnit from source code?

How to create CodeCompileUnit from source code?

What is the best way to parse C # source code (s)?

Is CodeCompileUnit the right choice? And How?

thank

+5
source share
3 answers

You have it the other way around, CodeCompileUnit exists to generate the source code. If you already have the source code, you only need a class that inherits CodeDomProvider to compile the code. Like Microsoft.CSharp.CSharpCodeProvider or Microsoft.VisualBasic.VBCodeProvider.

, . , System.CodeDom.Compiler.CodeParser. , . .

+7

. ? , ? - Stackoverflow. , :

+4

CodeSnippetCompileUnit, CodeCompileUnit:

string source = @"
using System;
namespace SomeNamespace 
{
  public class Class0
  {     
  }
}";

var csu0 = new CodeSnippetCompileUnit(curSource);

:

, :

CodeDomProvider provider = new CSharpCodeProvider();
CompilerResults results = provider.CompileAssemblyFromDom(new CompilerParameters(), csu0, csu1 /*arbitrary number*/);

, , CodeSnippetCompileUnit .

+4

All Articles