【发布时间】:2021-02-25 18:48:38
【问题描述】:
谁能帮助我,告诉我这段代码我做错了什么。文件中的输出应该是这样的:
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;
}
我尝试了很多次来修复它,但文件输出只是带出了一个空页面。我不知道顺序是否不正确,或者我是否犯了语法错误或输入顺序错误。我是动态编程的新手,所以我不知道该怎么做
【问题讨论】: