使用 CodeDOM

问题描述

有人可以帮助我并告诉我这段代码做错了什么吗?文件上的输出应该是这样的:

class Country {
        public string Name {get; set;}
        public int Population {get; set;}
       
        public Country(string name,int population){
                Name = name;
                Population = population;
        }
        public string GetCountryInfo(){
                return "Country" + Name + " has the population of " + Population + ".";
        }
} 

这是我的代码的样子:

namespace Assignment_Refleksija_2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creating Assembly
            CodeCompileUnit countryAssembly = new CodeCompileUnit();

            // Creating Namesapce
            CodeNamespace countryNamespace = new CodeNamespace("RefleksijaCountry");

            // Importing libraries
            countryNamespace.Imports.Add(new CodeNamespaceImport("System"));

            //Creating Class Country
            CodeTypeDeclaration countryClass = new CodeTypeDeclaration();
            countryClass.Name = "Country";
            countryClass.IsClass = true;
            countryClass.Attributes = MemberAttributes.Public;

            //Importing Class Country
            countryNamespace.Types.Add(countryClass);

            //Creating Property Name
            CodeMemberProperty propertyName = new CodeMemberproperty();
            propertyName.Name = "Name";
            propertyName.Type = new CodeTypeReference(typeof(System.String));
            propertyName.Attributes = MemberAttributes.Public;
            CodeSnippetExpression getSnippetName = new CodeSnippetExpression("return Name");
            CodeSnippetExpression setSnippetName = new CodeSnippetExpression("Name = value");
            propertyName.GetStatements.Add(getSnippetName);
            propertyName.SetStatements.Add(setSnippetName);

            //Creating Property Population
            CodeMemberProperty propertyPopulation = new CodeMemberproperty();
            propertyName.Name = "Name";
            propertyName.Type = new CodeTypeReference(typeof(system.int32));
            propertyName.Attributes = MemberAttributes.Public;
            CodeSnippetExpression getSnippetPopulation = new CodeSnippetExpression("return Population");
            CodeSnippetExpression setSnippetPopulation = new CodeSnippetExpression("Population = value");
            propertyName.GetStatements.Add(getSnippetPopulation);
            propertyName.SetStatements.Add(setSnippetPopulation);

            //Creating Country Method
            CodeMemberMethod methodCountry = new CodeMemberMethod();
            methodCountry.Name = "Country";
            CodeParameterDeclarationExpression name = new CodeParameterDeclarationExpression(typeof(string),"name");
            CodeParameterDeclarationExpression population = new CodeParameterDeclarationExpression(typeof(int),"population");
            methodCountry.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            CodeSnippetExpression snippetname = new CodeSnippetExpression("Name = name");
            CodeSnippetExpression snippetpopulation = new CodeSnippetExpression("Population = population");
            CodeExpressionStatement cse1 = new CodeExpressionStatement(snippetname);
            CodeExpressionStatement cse2 = new CodeExpressionStatement(snippetpopulation);
            methodCountry.Statements.Add(cse1);
            methodCountry.Statements.Add(cse2);

            //Creating GetCountryInfo Method
            CodeMemberMethod methodGetCountryInfo = new CodeMemberMethod();
            methodCountry.Name = "GetCountryInfo";
            CodeTypeReference returnTypestring = new CodeTypeReference(typeof(System.String));
            methodGetCountryInfo.ReturnType = returnTypestring;
            methodCountry.Attributes = MemberAttributes.Public;
            CodeSnippetExpression snippetone = new CodeSnippetExpression("return Name + Population");

            //Create File
            GenerateCSharpCode(countryAssembly,"HelloWorld");
        }

这就是文件生成器的样子

public static string GenerateCSharpCode(CodeCompileUnit compileunit,string fileName)
        {

            CSharpCodeProvider provider = new CSharpCodeProvider();

            string sourceFile;
            if (fileName.Equals(""))
            {
                sourceFile = "Untitled.cs";
            }
            else
            {
                sourceFile = fileName + ".cs";
            }

            // Create a TextWriter to a StreamWriter to the output file.
            using (StreamWriter sw = new StreamWriter(sourceFile,false))
            {
                IndentedTextWriter tw = new IndentedTextWriter(sw,"    ");

                // Generate source code using the code provider.
                provider.GenerateCodeFromCompileUnit(compileunit,tw,new CodeGeneratorOptions());

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

            return sourceFile;
        }

我尝试了很多次修复它,但文件输出只是显示一个空白页面。我不知道顺序是否不正确,或者我是否犯了语法错误错误的输入顺序。我是动态编程的新手,所以我不知道该怎么做

解决方法

您的问题可能是最后缓冲了您的作家。

StreamWriter 和 IndentedTextWriter 都被允许缓冲他们应该写的东西——而不是立即传递输入它们的每个字符,它们暂时在内存中累积它们,并且只有在缓冲区已满或他们被告知要“冲洗”。

而且...刷新以两种方式发生:1) 他们被明确告知通过 Flush() 方法或 2) 他们很好地处理(例如通过 using 块) .通常您不必考虑这些,因为 using 块会悄悄地为您刷新缓冲区。

...等等...

我注意到 IndentedTextWriter 不是 using 块的一部分,这意味着它没有被很好地处理。我敢打赌,它正在填满其缓冲区,并且在手动突然关闭之前实际上并未向底层 StreamWriter 写入任何内容。

解决方案?像这样(希望如此!):

using (var sw = new StreamWriter(sourceFile,false))
using (var tw = new IndentedTextWriter(...))
{
    provider.GenerateCodeFromCompileUnit(compileunit,tw,new CodeGeneratorOptions());
    
    //no explicit close needed

} //tw implicitly disposed here (ie flushed + closed)