C # basic --- Attribute and reflect (reflection) Application 2

Source: Internet
Author: User

C # basic --- Attribute and reflect (reflection) Application 2
Background: [it seems inappropriate to simulate a background for convenience, but I wrote this in my Demo. of course, the comparison with the company is definitely not perfect] Everyone eats, but the eating habits in every country are different. Chinese eat dumplings, Canadian eat pasta, American eat turkey. People from other countries eat other things. How can we maintain different customs in different countries. I opened a restaurant with food in China, the United States, and Canada. If a person comes, how can we let the program determine his or her nationality automatically. This requires tag and reflection. Let's take a look at the Demo. 1. entity class. First, we define a base class. The virtual method is defined in the Person base class, and everyone will eat. Person (Country = PersonCountry. UNKnown)] public class Person {public virtual void Eat () {Console. writeLine ("we eat") ;}} again, people from different countries inherit the base class and override the eat method to reflect the eating habits of different countries. [Person (Country = PersonCountry. USA)] public class USA: Person {public override void Eat () {Console. writeLine ("we eat turkey");} [Person (Country = PersonCountry. china)] public class Chinese: Person {public override void Eat () {Console. writeLine ("We eat dumplings");} [Person (Country = PersonCountry. USA)] public class USA: Person {public override void Eat () {Console. writeLine ("Let's eat turkey") ;}} 2. label to distinguish the Person in the country of each class, we set a Person tag, and the PersonCountry enumeration public class PersonAttribute: Attribute {private PersonCountry _ country = PersonCountry. UNKnown; public PersonCountry Country {get {return _ country;} set {this. _ country = value ;}} public enum Pers OnCountry {USA, China, Canada, UNKnown} 3. Helper method: the entity class and enumeration have been defined. So how can we let the program automatically differentiate people's nationality and then confirm their eating habits. The following can be determined through reflection. a. assembly. getExecutingAssembly () can obtain the current program level, assembly. getTypes () obtains all types, including USA, Canda, and Chinese Information B. (PersonAttribute) Attribute. getCustomAttribute (currentType, typeof (PersonAttribute); obtain the tag of the current class, and then compare the tag with the passed countryType to confirm the Person type. c. then, the InvokeMember is used to create the object, and the public class PersonHelper {public static Person GetPersonObj (PersonCountry contryType) {Assembly assembly = Assembly is returned. getExecuti NgAssembly (); Type [] typeList = assembly. getTypes (); foreach (Type currentType in typeList) {var attribute = (PersonAttribute) Attribute. getCustomAttribute (currentType, typeof (PersonAttribute); if (attribute! = Null & attribute. country = contryType) {return currentType. invokeMember (null, BindingFlags. public | BindingFlags. nonPublic | BindingFlags. instance | BindingFlags. createInstance, null, null, new object [] {}) as Person ;}return new Person () ;}} 4. test method: You may have been confused before. Let's take a look at the test below. Note: 1. if the new user is a Chinese user, you only need to call the Helper method, obtain the Chinese object, and then execute the corresponding eat method. 2. If we want to know about Korean eating habits, we only need to add one more class and then add the corresponding label. Public static void Main (string [] args) {Person p1 = PersonHelper. getPersonObj (PersonCountry. china); p1.Eat (); // output: We eat dumplings Person p2 = PersonHelper. getPersonObj (PersonCountry. canada); p2.Eat (); // output: pasta for us} 5. summary: The examples described in this blog are far-fetched, but I just feel that this idea is still good. This facilitates project expansion. I hope it will be useful to you. I also want to write this blog to review reflection and tag usage.

Related Article

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.