当前位置: 首页>>代码示例>>C#>>正文


C# CompilerError类代码示例

本文整理汇总了C#中System.CodeDom.Compiler.CompilerError的典型用法代码示例。如果您正苦于以下问题:C# CompilerError类的具体用法?C# CompilerError怎么用?C# CompilerError使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


CompilerError类属于System.CodeDom.Compiler命名空间,在下文中一共展示了CompilerError类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetCompileUnit

//引入命名空间
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace CompilerError_Example
{
    public class Class1
    {		
        [STAThread]
        static void Main(string[] args)			
        {
            // Output some program information using Console.WriteLine.
            Console.WriteLine("This program compiles a CodeDOM program that incorrectly declares multiple data");
            Console.WriteLine("types to demonstrate handling compiler errors programmatically.");
            Console.WriteLine("");
                        
            // Compile the CodeCompileUnit retrieved from the GetCompileUnit() method.
            CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
                                               
            // Initialize a CompilerParameters with the options for compilation.
            string[] assemblies = new String[] {"System.dll"};
            CompilerParameters options = new CompilerParameters( assemblies, "output.exe");

            // Compile the CodeDOM graph and store the results in a CompilerResults.
            CompilerResults results = provider.CompileAssemblyFromDom(options, GetCompileUnit());
                                        
            // Compilation produces errors. Print out each error.
            Console.WriteLine("Listing errors from compilation: ");
            Console.WriteLine("");
                for( int i=0; i<results.Errors.Count; i++)
                Console.WriteLine(results.Errors[i].ToString());			
        }

        public static CodeCompileUnit GetCompileUnit()
        {
            // Create a compile unit to contain a CodeDOM graph.
            CodeCompileUnit cu = new CodeCompileUnit();

            // Create a namespace named TestSpace.
            CodeNamespace cn = new CodeNamespace("TestSpace");		
        
            // Declare a new type named TestClass.	
            CodeTypeDeclaration cd = new CodeTypeDeclaration("TestClass");

            // Declare a new member string field named TestField.
            CodeMemberField cmf = new CodeMemberField("System.String", "TestField");

            // Add the field to the type.
            cd.Members.Add(cmf);

            // Declare a new member method named TestMethod.
            CodeMemberMethod cm = new CodeMemberMethod();
            cm.Name = "TestMethod";

            // Declare a string variable named TestVariable.
            CodeVariableDeclarationStatement cvd = new CodeVariableDeclarationStatement("System.String1", "TestVariable");
            cm.Statements.Add(cvd);

            // Cast the TestField reference expression to string and assign it to the TestVariable.
            CodeAssignStatement ca = new CodeAssignStatement(new CodeVariableReferenceExpression("TestVariable"), 
                new CodeCastExpression("System.String2", new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "TestField")));

            // This code can be used to generate the following code in C#:
            //            TestVariable = ((string)(this.TestField));

            cm.Statements.Add(ca);
             
            // Add the TestMethod member to the TestClass type.
            cd.Members.Add(cm);
        
            // Add the TestClass type to the namespace.
            cn.Types.Add(cd);
            
            // Add the TestSpace namespace to the compile unit.
            cu.Namespaces.Add(cn);
            return cu;
        }
    }
}
开发者ID:.NET开发者,项目名称:System.CodeDom.Compiler,代码行数:81,代码来源:CompilerError


注:本文中的System.CodeDom.Compiler.CompilerError类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。