In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use Attribute in C#". In daily operation, I believe many people have doubts about how to use Attribute in C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Attribute in C#". Next, please follow the editor to study!
First, create the attribute [AttributeUsage (AttributeTargets.Class | AttributeTargets.Constructor, AllowMultiple = true, Inherited = true)] / / AttributeTargets: the target type to which the attribute is applied. AllowMultiple: whether to allow more than one attribute to be applied to an element. Inherited: whether an attribute can be inherited by a derived class. Public class CodeStatusAttribute: the Attribute {private string status; public CodeStatusAttribute (string status) / / constructor is the position parameter {this.status = status;} public string Tester {set; get;} / / attribute and the public field is the named parameter public string Coder {set; get;} public override string ToString () {return status 2. Apply attribute / / 1, use single attribute [CodeStatus ("a version")] public class Tringe {} / / 2, use multiple attributes [CodeStatus ("b version", Coder= "Xiao Li")] [CodeStatus ("b version", Coder= "Xiao Wang")] / / can also [CodeStatus ("aa", Coder= "Xiao Li"), CodeStatus ("aa") Coder= "Xiao Wang")] public class Square {} / / 3, use the location parameter and the named parameter / / type to indicate what element this attribute is associated with There may be: assembly,field,method,param,property,return,moudule,event,type et al. [type: CodeStatus ("final version", Coder = "Xiao Li", Tester = "Lao Li")] public class Circle {[CodeStatus ("final version", Coder = "Xiao Li", Tester = "Lao Li")] public Circle () {}} III, reflection attribute / / 1, get the attribute on the class. Type t = typeof (Circle); Attribute [] attArr = Attribute.GetCustomAttributes (t, typeof (CodeStatusAttribute)); / / or object [] attArr1 = t.GetCustomAttributes (typeof (CodeStatusAttribute), true); / / 2. Get the attribute on the member Attribute [] attArr3 = t.GetConstructors () [0] .GetCustomAttributes (). ToArray (); / / constructor, get the field GetField ("..") / 3, traverse foreach (Attribute attr in attArr3) {CodeStatusAttribute item = (CodeStatusAttribute) attr Console.Write (item.ToString () + item.Coder + item.Tester);} IV, Net built-in attribute [Condeitonal] / / condition control [Obsolete] / / obsolete attribute [Serializable] / / Serializable attribute [AssemblyDelaySign] / / Assembly delay signature here, on the "C# how to use Attribute" study is over, hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.