مشاركة عبر


إنشاء تعليمات برمجية المصدر و ترجمة برنامج من رسم CodeDOM

System.CodeDom.Compilerتوفر مساحة الاسم الواجهات توليد تعليمات برمجية المصدر من الرسوم البيانية للكائن CodeDOM ولإدارة الترجمة مع compilers المعتمدة. إنتاج مورد التعليمات برمجية تعليمات برمجية المصدر في لغة برمجة معينة استناداً إلى رسم بياني CodeDOM. A فئة that derives من CodeDomProvider can typically provide وظائف for generating و جاري التحويل البرمجي تعليمات برمجية for the اللغة the موفر supports.

Using a CodeDOM تعليمات برمجية موفر إلى توليد المصدر تعليمات برمجية

لإنشاء تعليمات برمجية المصدر في معينة اللغة، تحتاج إلى رسم بياني CodeDOM الذي يمثل بنية تعليمات برمجية المصدر لإنشاء.

شرح المثال التالي كيف إلى إنشاء مثيل ل CSharpCodeProvider:

Dim provider As New CSharpCodeProvider()
CSharpCodeProvider provider = new CSharpCodeProvider();

الرسم البياني لإنشاء تعليمات برمجية هو عادة المحتواة في CodeCompileUnit. إلى إنشاء تعليمات برمجية ل CodeCompileUnit يحتوي على رسم بياني CodeDOM، استدعاء GenerateCodeFromCompileUnitأسلوب الموفر تعليمات برمجية. Th هو أسلوب يحتوي على معلمة ل TextWriterأن يستخدم لإنشاء تعليمات برمجية المصدر، ولذلك هو في بعض الأحيان، من الضروري أولاً بإنشاء TextWriter التي يمكن كتابتها إلى. يوضح المثال التالي توليد تعليمات برمجية من CodeCompileUnit وكتابة التعليمات البرمجية المصدر الذي تم إنشاؤه إلى ملف يسمى HelloWorld.cs.

Public Shared Function GenerateCSharpCode( _
    compileunit As CodeCompileUnit) As String
       
    ' Generate the code with the C# code provider.
    Dim provider As CSharpCodeProvider = New CSharpCodeProvider()

    ' Build the output file name.
    Dim sourceFile As String
    If provider.FileExtension.StartsWith(".")
        sourceFile = "HelloWorld" + provider.FileExtension
    Else
        sourceFile = "HelloWorld." + provider.FileExtension
    End If

    ' Create a TextWriter to a StreamWriter to an output file.
    Dim tw As New IndentedTextWriter( _
        New StreamWriter(sourceFile, False), "    ")

    ' Generate source code using the code provider.
    provider.GenerateCodeFromCompileUnit(compileunit, tw, _
        New CodeGeneratorOptions())

    ' Close the output file.
    tw.Close()
        
    Return sourceFile
End Function
   
public static String GenerateCSharpCode(CodeCompileUnit compileunit)
{
    // Generate the code with the C# code provider.
    CSharpCodeProvider provider = new CSharpCodeProvider();

    // Build the output file name.
    String sourceFile;
    if (provider.FileExtension[0] == '.')
    {
        sourceFile = "HelloWorld" + provider.FileExtension;
    }
    else 
    {
        sourceFile = "HelloWorld." + provider.FileExtension;
    }

    // Create a TextWriter to a StreamWriter to the output file.
    IndentedTextWriter tw = new IndentedTextWriter(
            new StreamWriter(sourceFile, false), "    ");
            
    // Generate source code using the code provider.
    provider.GenerateCodeFromCompileUnit(compileunit, tw, 
            new CodeGeneratorOptions());

    // Close the output file.
    tw.Close();            

    return sourceFile;
}
   

استخدام DOM تعليمات برمجية تعليمات برمجية موفر التحويل البرمجي للتجميعات

تجميع الاستدعاء

إلى التحويل البرمجي تجميع استخدام موفر إليه CodeDom، يجب أن يكون لديك تعليمات برمجية المصدر أما إلى التحويل البرمجي للغة التي لديها مترجم، أو تلك تعليمات برمجية المصدر إنشاء رسم بياني CodeDOM إلى الترجمة التي يمكن إنشاؤها من.

في حالة جاري التحويل البرمجي من رسم بياني CodeDOM، تمرير CodeCompileUnitالذي يحتوي على الرسم البياني إلى CompileAssemblyFromDomأسلوب الموفر تعليمات برمجية. إذا كان لديك ملف التعليمات برمجية مصدر في اللغة أن المحول البرمجي يفهم، تمرير اسم الملف الذي يحتوي على تعليمات برمجية المصدر إلى CompileAssemblyFromFileأسلوب موفر إليه CodeDom. يمكنك أيضا تمرير سلسلة أحرف يحتوي على تعليمات برمجية المصدر في لغة يفهمه المحول البرمجي إلى CompileAssemblyFromSourceأسلوب موفر إليه CodeDom.

تكوين معلمات التحويل البرمجي

تحتوي الجميع الأساليب القياسية استدعاء بتصنيف موفر إليه CodeDom معلمة من نوع CompilerParametersالذي يشير إلى الخيارات استخدم للتحويل البرمجي.

يمكنك تحديد ملف اسم للتجميع الإخراج في OutputAssemblyخاصية من CompilerParameters. وإلا، الافتراضي سيتم استخدام اسم ملف الإخراج.

بشكل افتراضي على جديد من CompilerParameters هو تهيئة مع به GenerateExecutableتعيين خاصية إلى خطأ . إذا كنت يتم ترجمة برنامج تنفيذي، عليك تعيين GenerateExecutable خاصية إلى صحيحة . عند من GenerateExecutable هو معينة إلى خطأ ، المترجم سيقوم بإنشاء مكتبة فئة.

إذا كنت يتم التحويل البرمجي لملحق تنفيذي من رسم بياني CodeDOM CodeEntryPointMethodيجب تعريف في الرسم البياني. إذا كان هناك عدة تعليمات برمجية نقاط الإدخال، فقد يكون ذلك ضروريا لتعيين MainClassخاصية CompilerParameters إلى اسم الفئة التي تعرف يؤشر إدخال لاستخدام.

لتضمين معلومات يصحح في الملف القابل للتنفيذ الذي تم إنشاؤه، قم بتعيين صوابIncludeDebugInformation خاصية إلى .

إذا كان مشروع الخاص بك بالرجوع إلى أي تجميعات، يجب تحديد أسماء تجميع كعناصر في StringCollection ReferencedAssembliesخاصية من CompilerParameters الذي استخدم عند استدعاء التحويل البرمجي.

يمكن أن تقوم بالتحويل البرمجي تجميع الذي هو كتابتها إلى ذاكرة بدلاً من dهوk بتعيين GenerateInMemoryخاصية إلى صحيحاً . عند تجميع هو التي تم إنشاؤها في ذاكرة، التعليمات البرمجية يمكن الحصول على مرجع إلى التجميع الذي تم إنشاؤه من CompiledAssemblyخاصية CompilerResults. في حالة تجميع هو كتابة dهوk، يمكنك الحصول على مسار إلى التجميع الذي تم إنشاؤه من PathToAssemblyخاصية من CompilerResults.

إلى تحديد سلسلة وسائط سطر أوامر cusإلىm إلى عند استدعاء عملية الترجمة، قم بتعيين السلسلة في CompilerOptionsخاصية.

إذا تم طلب رمز الأمان مميز Win32 لاستدعاء العملية المترجم، حدد الرمز المميز في UserTokenخاصية.

إلى ارتباط a Win32 مورد ملف في the compiled تجميع, specify the اسم of the Win32 مورد ملف in the Win32Resource خاصية.

إلى specify a تحذير المستوى at which إلى halt compilation, التعيين the WarningLevel خاصية إلى an عدد صحيح that represents the تحذير المستوى at which إلى halt compilation. You can also تكوين the compiler إلى halt compilation if تحذيرات are encountered بواسطة إعداد the TreatWarningsAsErrors خاصية إلى صواب.

The following تعليمات برمجية مثال demonstrates جاري التحويل البرمجي a الملف المصدر using a CodeDom موفر derived من the CodeDomProvider فئة.

Public Shared Function CompileCSharpCode(sourceFile As String, _
    exeFile As String) As Boolean
    Dim provider As CSharpCodeProvider = New CSharpCodeProvider()
          
    ' Build the parameters for source compilation.
    Dim cp As New CompilerParameters()
         
    ' Add an assembly reference.
    cp.ReferencedAssemblies.Add("System.dll")

    ' Save the assembly as a physical file.
    cp.GenerateInMemory = False

    ' Generate an executable instead of a class library.
    cp.GenerateExecutable = True
          
    ' Set the assembly file name to generate.
    cp.OutputAssembly = exeFile
        
    ' Invoke compilation.
    Dim cr As CompilerResults = _
        provider.CompileAssemblyFromFile(cp, sourceFile)
          
    If cr.Errors.Count > 0 Then
        ' Display compilation errors.
        Console.WriteLine("Errors building {0} into {1}", _
            sourceFile, cr.PathToAssembly)
        Dim ce As System.CodeDom.Compiler.CompilerError
        For Each ce In  cr.Errors
            Console.WriteLine("  {0}", ce.ToString())
            Console.WriteLine()
        Next ce
    Else
        Console.WriteLine("Source {0} built into {1} successfully.", _
            sourceFile, cr.PathToAssembly)
    End If
          
    ' Return the results of compilation.
    If cr.Errors.Count > 0 Then
        Return False
    Else
        Return True
    End If
End Function
public static bool CompileCSharpCode(String sourceFile, 
    String exeFile)
{
    CSharpCodeProvider provider = new CSharpCodeProvider();

    // Build the parameters for source compilation.
    CompilerParameters cp = new CompilerParameters();

    // Add an assembly reference.
    cp.ReferencedAssemblies.Add( "System.dll" );

    // Generate an executable instead of 
    // a class library.
    cp.GenerateExecutable = true;

    // Set the assembly file name to generate.
    cp.OutputAssembly = exeFile;

    // Save the assembly as a physical file.
    cp.GenerateInMemory = false;
 
    // Invoke compilation.
    CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile);

    if(cr.Errors.Count > 0)
    {
        // Display compilation errors.
        Console.WriteLine("Errors building {0} into {1}",  
            sourceFile, cr.PathToAssembly);
        foreach(CompilerError ce in cr.Errors)
        {
            Console.WriteLine("  {0}", ce.ToString());
            Console.WriteLine();
        }
    }
    else
    {
        Console.WriteLine("Source {0} built into {1} successfully.",
            sourceFile, cr.PathToAssembly);
    }
          
    // Return the results of compilation.
    if (cr.Errors.Count > 0)
    {
        return false;
    }
    else 
    {
        return true;
    }
}

Languages مع Initial دعم

The .NET Framework provides تعليمات برمجية compilers و تعليمات برمجية generators for the following languages: C#, Visual أساسى, C++, J#, و JScript. CodeDOM دعم can be extended إلى لغات غير ذلك بواسطة implementing خاص بلغة تعليمات برمجية generators و تعليمات برمجية compilers.

راجع أيضًا:

المرجع

مرجع سريع codeDOM

System.CodeDom

System.CodeDom.Compiler

موارد أخرى

إنشاء التعليمة البرمجية المصدر الحيوي والترجمة