I used to write about a tag-and-reflex application, primarily for validation of fields in a class. The following are links to the C # Foundation---Attribute (label) and reflect (reflection) applications. This project iteration found that the company projects found in the old code on the business expansion of the place and by marking and reflection made a small framework, feel very practical. So think of a copy and then move to the blog park. May not write well, also please bo friends pointing.
Background: "For convenience, I still imitate a background bar, feeling is not very appropriate, but their own demo inside of this write." Of course, compared with the company is definitely not perfect.
Everyone eats, but the habit of eating in every country is different. Chinese people eat glutinous rice balls, Canadians eat pasta, and Americans eat turkey. There are other people in other countries eating other things. How to maintain different customs in different countries. I opened a restaurant, there are Chinese, American, Canadian food, if a person, how we let the program automatically through the person's nationality, to determine what he is to eat. This is going to be solved by playing labels and reflections. Let's look at the demo.
1. Entity classes.
First we define a base class in which a virtual method is defined inside the person base class, and everyone eats something.
Person (country=personcountry.unknown)] publicclass person { Public Virtual void Eat () { Console.WriteLine (" we eat something "); } }
Again, people from different countries, to inherit the base class, rewrite the Eat method, reflect the different countries eating habits.
[Person (country=Personcountry.usa)] Public classUsa:person { Public Override voidEat () {Console.WriteLine ("we eat Turkey ."); }} [Person (Country=Personcountry.china)] Public classChinese:person { Public Override voidEat () {Console.WriteLine ("we eat glutinous rice balls"); }} [Person (Country=Personcountry.usa)] Public classUsa:person { Public Override voidEat () {Console.WriteLine ("we eat Turkey ."); } }
2. Tags
In order to differentiate the individual of each class in which country, we set a person tag, and the Personcountry enumeration
Public classPersonattribute:attribute {PrivatePersoncountry _country =Personcountry.unknown; PublicPersoncountry Country {Get { return_country; } Set { This. _country =value; } } } Public enumpersoncountry {USA, China, Canada, UNKnown}
3. Helper methods:
Entity classes and enumerations are already defined. How to make the program automatically differentiate, people's nationality, and then confirm their eating habits. The following can be judged by reflection.
A. assembly.getexecutingassembly () can get the current program level, Assembly. GetTypes () Get all types, including Usa,canda,chinese information
B. (personattribute) Attribute.GetCustomAttribute (Currenttype, typeof(Personattribute)); Gets the label of the current class, Then use the tag to compare with the incoming countrytype to confirm the person type.
C. Then create the object via InvokeMember and return
Public classPersonhelper { Public StaticPerson getpersonobj (personcountry contrytype) {Assembly Assembly=assembly.getexecutingassembly (); Type[] Typelist=Assembly. GetTypes (); foreach(Type Currenttypeinchtypelist) { varattribute = (personattribute) attribute.getcustomattribute (Currenttype,typeof(Personattribute)); if(attribute!=NULL&& attribute. Country = =Contrytype) { returnCurrenttype.invokemember (NULL, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | Bindingflags.createinstance,NULL,NULL,New Object[] { }) asPerson ; } } return NewPerson (); } }
4. Test method:
May be in front of people stumbled, below to see the test, may be more clear:
Note:1. If the new person is Chinese, just call the helper method and get to the Chinese object, then execute the corresponding eat method.
2. If we want to know the Korean eating habits, only need to add one more class, and then hit the corresponding label on the OK.
Public Static void Main (string[] args) { = personhelper.getpersonobj (personcountry.china); P1. Eat (); Output: We eat glutinous rice balls = personhelper.getpersonobj (personcountry.canada); P2. Eat (); Output: We eat pasta }
5. Summary:
This blog describes the example is very far-fetched, but just feel that the idea is still good. Facilitates the extension of the project. Hope to be useful to everyone. At the same time also is oneself in order to review the reflection and the use of tag, only intends to write this blog.
C # Basic---Attribute (label) and reflect (reflection) application Two