consoleapplication--Console Application
First create the base class:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceattribute_exercise{/// <summary> ///comments here do not affect compilation and operation, only help developers/// </summary> //[some Attribute] e.g.[Obsolete]//[Obsolete], [Obsolete (")], [Obsolete (" ", True/false)] Obsolete's constructor F12 view[Serializable] Public classBasicclass {//base class properties, Fields Public intID {Set;Get; } Public stringName {Set;Get; } } Public classAclass:basicclass {} Public classBclass:basicclass {} Public classcclass:basicclass {}}
View Code
Next, customize a attribute:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceattribute_exercise{/// <summary> ///Customizing a Attribute:somename+attribute///when used: [Somename]/// </summary> Public classCustomizedattribute:attribute {//The following are the constructor functions PublicCustomizedattribute () {//Empty } PublicCustomizedattribute (stringmsg) { } PublicCustomizedattribute (stringMsgBOOLerr) { } //Field Public stringMSG; Public BOOLERR; } Public classEslecusomizedattribute:attribute {}}
View Code
In the main portal:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceattribute_exercise{classProgram {Static voidMain (string[] args) { //The main use of the inheritance mechanismBasicclass BC =NewBasicclass () {id=1, name="base class 1"}; Console.WriteLine ("base class id:{0}; base class class name: {1}", BC.ID,BC. Name); Console.ReadLine (); } }}
View Code
First look at the effect of the base class:
The following goes into the topic-How do you make these inheriting classes behave differently or effectively without changing the base class and its inheriting classes, simply by declaring attribute?
1. Statement attribute: added [customized] at BasicClass.cs line 23;
2. How to add the corresponding attribute:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Reflection;//ReflectionusingSystem.Text;usingSystem.Threading.Tasks;namespaceattribute_exercise{ Public classattributeactivities { Public Static voidOutput<t> (T t)wheret:basicclass {Type type=T.gettype (); Attribute Attribute= Type. GetCustomAttribute (typeof(Customizedattribute));//note using System.Reflection; if(Attribute! =NULL&& attribute isCustomizedattribute) {Customizedattribute customizedattr= attribute asCustomizedattribute; Console.WriteLine ("the current class extra attribute:{0}", Customizedattr.tostring (). Substring (Customizedattr.tostring (). IndexOf (".")+1)); Console.WriteLine ("+++++++++++++++++++++++++++++++++++++++++++++++++"); } } }}
View Code
3. Invoke the method at the main entrance of the program:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceattribute_exercise{classProgram {Static voidMain (string[] args) { //The main use of the inheritance mechanismBasicclass BC =NewBasicclass () {id=1, name="base class 1"}; Console.WriteLine ("base class id:{0}; base class class name: {1}", BC.ID,BC. Name); Console.WriteLine ("*************************************************"); Attributeactivities.output<AClass> (NewAClass () {ID=2, Name="AClass" }); Console.WriteLine ("*************************************************"); Attributeactivities.output<BClass> (NewBclass () {ID=3, Name="Bclass" }); Console.WriteLine ("*************************************************"); Attributeactivities.output<CClass> (NewCclass () {ID=4, Name="Cclass" }); Console.ReadLine (); } }}
View Code
Results:
Does it feel magical? There is no modification to this inheriting class, which is a declaration, and then attaching the corresponding method implements the different attribute classes with different effects.
Readers can also expand their own other ways to try ...
NOTE: No reprint is permitted without the consent of the author! Reprint please explain the source! Please respect intellectual property rights.
Change the output effect of different classes by declaring the attribute property