Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Attribute in C #

Shulou Source: shulou.com Published: 2022-05-31 17:47:12 10月03日 Update

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!

Tags: Attributes attributes parameters Xiao Li Learning Application location elements functions multiple Fields more Xiao Wang Lao Li help practical next single sequence member Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Microsoft vpn Shulou Tech Info OPPO Reno