Scene:
There are currently two entity classes: Student,classinfo.
public class Student {
public string Name {get; set;}
public string Sex {get; set;}
public string Age {get; set;}
}
public class ClassInfo
{
public string Subject {get; set;}
public int score {get; set;}
}
New Student () {name=" Zhang San ", sex=" male ", age=" " New ClassInfo () {subject=" language ", score=);
You want the concatenation to generate a string of dynamic strings (not using string. Format). The attributes inside can be added, deleted, and arbitrarily changed in order. Such as:
--Scenario 1stringmsg ="student: [Student.name],[student.sex],[student.age] years old. Course score: [Classinfo.subject]:[classinfo.score].";---results output After processing students: Zhang San, male, 18 years old. Course Score: Chinese: the.--Scenario 2stringmsg ="student: [Student.name],[student.age] years old. Course score: [Classinfo.subject]:[classinfo.score].";---results output After processing students: Zhang San, 18 years old. Course Score: Chinese: the.--Scenario 2stringmsg ="[ Student.age] years [student.name] [Classinfo.subject] Score: [Classinfo.score].";---results of processing after the output of the 18-year-old Zhang San language score: the.
Processing method:
Student stu =NewStudent () {name="Zhang San", sex="male", age=" -"}; ClassInfo CLA=NewClassInfo () {subject="language", score= the};Object[] sources =New Object[] {STU,CLA};stringmsg ="student: [Student.name],[student.sex],[student.age] years old. Course score: [Classinfo.subject]:[classinfo.score]."; foreach(varSourceinchsources) { if(Source = =NULL) Continue; Msg=messagebodyfactory.getmessagebody (source, msg);}
You can modify the MSG as you wish.
Source Address: Http://files.cnblogs.com/gguozhenqian/ConsoleApplication1.zip
C # dynamically generates strings based on attributes of entity classes