添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
重情义的鸵鸟  ·  Python IDLE Shell ...·  2 年前    · 
曾经爱过的皮蛋  ·  Unable to create ...·  2 年前    · 
大力的馒头  ·  Python SQLite: ...·  2 年前    · 
满身肌肉的皮带  ·  c++ - error: command ...·  2 年前    · 
public ref class CSharpCodeProvider : System::CodeDom::Compiler::CodeDomProvider
public class CSharpCodeProvider : System.CodeDom.Compiler.CodeDomProvider
type CSharpCodeProvider = class
    inherit CodeDomProvider
Public Class CSharpCodeProvider
Inherits CodeDomProvider
CSharpCodeProvider

下列範例會使用 C# 或 Visual Basic程式碼提供者來編譯原始程式檔。 此範例會檢查輸入副檔名,並使用對應的 CSharpCodeProvider VBCodeProvider 進行編譯。 輸入檔會編譯成可執行檔,而且任何編譯錯誤都會顯示在主控台中。

using System; using System.IO; using System.Globalization; using System.CodeDom.Compiler; using System.Text; using Microsoft.CSharp; using Microsoft.VisualBasic; namespace CodeProviders class CompileSample [STAThread] static void Main(string[] args) if (args.Length > 0) // First parameter is the source file name. if (File.Exists(args[0])) CompileExecutable(args[0]); Console.WriteLine("Input source file not found - {0}", args[0]); Console.WriteLine("Input source file not specified on command line!"); public static bool CompileExecutable(String sourceName) FileInfo sourceFile = new FileInfo(sourceName); CodeDomProvider provider = null; bool compileOk = false; // Select the code provider based on the input file extension. if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS") provider = CodeDomProvider.CreateProvider("CSharp"); else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB") provider = CodeDomProvider.CreateProvider("VisualBasic"); Console.WriteLine("Source file must have a .cs or .vb extension"); if (provider != null) // Format the executable file name. // Build the output assembly path using the current directory // and <source>_cs.exe or <source>_vb.exe. String exeName = String.Format(@"{0}\{1}.exe", System.Environment.CurrentDirectory, sourceFile.Name.Replace(".", "_")); CompilerParameters cp = new CompilerParameters(); // Generate an executable instead of // a class library. cp.GenerateExecutable = true; // Specify the assembly file name to generate. cp.OutputAssembly = exeName; // Save the assembly as a physical file. cp.GenerateInMemory = false; // Set whether to treat all warnings as errors. cp.TreatWarningsAsErrors = false; // Invoke compilation of the source file. CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceName); if(cr.Errors.Count > 0) // Display compilation errors. Console.WriteLine("Errors building {0} into {1}", sourceName, cr.PathToAssembly); foreach(CompilerError ce in cr.Errors) Console.WriteLine(" {0}", ce.ToString()); Console.WriteLine(); // Display a successful compilation message. Console.WriteLine("Source {0} built into {1} successfully.", sourceName, cr.PathToAssembly); // Return the results of the compilation. if (cr.Errors.Count > 0) compileOk = false; compileOk = true; return compileOk; Imports System.IO Imports System.Globalization Imports System.CodeDom.Compiler Imports System.Text Imports Microsoft.CSharp Namespace CodeProviders Class CompileSample <STAThread()> _ Public Shared Sub Main(args() As String) If args.Length > 0 ' First parameter is the source file name. If File.Exists(args(0)) CompileExecutable(args(0)) Console.WriteLine("Input source file not found - {0}", _ args(0)) End If Console.WriteLine("Input source file not specified on command line!") End If End Sub Public Shared Function CompileExecutable(sourceName As String) As Boolean Dim sourceFile As FileInfo = New FileInfo(sourceName) Dim provider As CodeDomProvider = Nothing Dim compileOk As Boolean = False ' Select the code provider based on the input file extension. If sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".CS" provider = CodeDomProvider.CreateProvider("CSharp") ElseIf sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".VB" provider = CodeDomProvider.CreateProvider("VisualBasic") Console.WriteLine("Source file must have a .cs or .vb extension") End If If Not provider Is Nothing ' Format the executable file name. ' Build the output assembly path using the current directory ' and <source>_cs.exe or <source>_vb.exe. Dim exeName As String = String.Format("{0}\{1}.exe", _ System.Environment.CurrentDirectory, _ sourceFile.Name.Replace(".", "_")) Dim cp As CompilerParameters = new CompilerParameters() ' Generate an executable instead of ' a class library. cp.GenerateExecutable = True ' Specify the assembly file name to generate. cp.OutputAssembly = exeName ' Save the assembly as a physical file. cp.GenerateInMemory = False ' Set whether to treat all warnings as errors. cp.TreatWarningsAsErrors = False ' Invoke compilation of the source file. Dim cr As CompilerResults = provider.CompileAssemblyFromFile(cp, _ sourceName) If cr.Errors.Count > 0 ' Display compilation errors. Console.WriteLine("Errors building {0} into {1}", _ sourceName, cr.PathToAssembly) Dim ce As CompilerError For Each ce In cr.Errors Console.WriteLine(" {0}", ce.ToString()) Console.WriteLine() Next ce ' Display a successful compilation message. Console.WriteLine("Source {0} built into {1} successfully.", _ sourceName, cr.PathToAssembly) End If ' Return the results of the compilation. If cr.Errors.Count > 0 compileOk = False compileOk = True End If End If return compileOk End Function End Class End Namespace

這個類別提供可用來擷取 C# ICodeGenerator 實例和 ICodeCompiler 實作的方法。

此類別包含適用于所有成員之類別層級的連結需求和繼承需求。 SecurityException 當立即呼叫端或衍生類別沒有完全信任許可權時,就會擲回 。