C # dynamically generates strings based on attributes of entity classes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.