繁体   English   中英

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

[英]Making a dynamic method Country and Population using CodeDOM

有人可以帮助我并告诉我这段代码我做错了什么。 Output 在文件上应该如下所示:

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;
        }

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

您的问题可能与最后缓冲您的作家有关。

StreamWriter 和 IndentedTextWriter 都被允许缓冲它们应该写入的内容——而不是立即传递输入它们的每个字符,它们将它们临时累积在 memory 中,并且仅在缓冲区已满或已满时才实际写入告诉“冲洗”。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM