使用 CodeDOM 制作动态方法 Country and Population

Making a dynamic method Country and Population using 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 都被允许缓冲它们应该写入的内容——它们不是立即传递输入它们的每个字符,而是将它们暂时累积在内存中,并且只有在缓冲区被写入时才真正写入满了或者他们被告知 'flush'.

并且...刷新以两种方式发生: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)