Translation link: https://www.infoworld.com/art...
Attribute is a very powerful feature in C\.
Attribute is actually an object, which can be associated with any of the following elements: assembly, class, method, delegate, enumeration, event, field, interface, attribute and structure. It will make information declaration on these objects. After the program runs, you can obtain the attribute information associated with these objects through reflection, In other words: you can inject some additional information into the assembly through Atrribute, and then obtain it through reflection at runtime. Attribute is generally composed of name + some optional parameters, and the attribute name corresponds to the Atrribute class.
You can use attributes to verify the correctness of your business model. There are two types of attributes: built-in + custom. The former is net framework, which needs to inherit system attribute class to implement customization.
Now let's look at how the code is written. The Obsolete feature is used to mark a method as Obsolete. This Obsolete means that you should no longer use this method. In the future, the framework will also eliminate it. At present, there are alternatives. In fact, there are many such examples in third-party frameworks. The following code snippet shows how to use the Obsolete feature at the top of the method.
[Obsolete("This method is obsolete...")] public static void DoSomeWork() { }
If you call this method in the program, some warning messages will appear in the Visual Studio output window when you compile the code, as shown in the following figure:
Of course, it's ok if you have to ignore it. Now, if you want your development colleagues not to allow you to call this method, how can you limit it? Ha ha, you can use the second parameter of Obsolete. This parameter is optional. The following is the modified version of DoSomeWork() method. Please note that this is a Boolean parameter.
[Obsolete("This method is obsolete...", true)] public static void DoSomeWork() { }
When you give true to this optional parameter and compile the code again, you will find that the code fails to compile at all. Does it solve your problem perfectly! The screenshot is as follows:
Custom attribute
In this section, let's take a look at how to implement a custom attribute. To customize the implementation, you can create a class and inherit system Attribute class, as shown in the following code:
using System; public class CustomAttribute : Attribute { }
To restrict the use of CustomAttribute, you can mark it with the AttributeUsage class, which contains the following attributes: ValidOn, AllowMultiple,Inherited, etc. these tags can restrict the use of CustomAttribute.
The following code snippet shows a modified version of CustomAttribute. This class uses a constructor to assign an internal private string. The code is only used for demonstration purposes.
[AttributeUsage(AttributeTargets.All)] public class CustomAttribute : Attribute { private string text; public CustomAttribute(string text) { this.Text = text; } public string Text { get => text; set => text = value; } }
Of course, you can also specify these AttributeTargets as required, as shown in the following code:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] public class CustomAttribute : Attribute { private string text; public CustomAttribute(string text) { this.Text = text; } public string Text { get => text; set => text = value; } }
Next, you can use reflection to obtain all attributes applied to the object. The code is as follows:
static void Main(string[] args) { MemberInfo memberInfo = typeof(CustomAttribute); object[] attributes = memberInfo.GetCustomAttributes(true); for (int i = 0, j = attributes.Length; i < j; i++) { Console.WriteLine(attributes[i]); } }
Next, I am going to apply the CustomAttribute class to the following SomeClass class.
[CustomAttribute("Hello World...")] public class SomeClass { }
You can focus on how the CustomAttribute is placed on SomeClass, and I also passed a Hello World The following code shows how to print the Text attribute in CustomAttribute.
class Program { static void Main(string[] args) { MemberInfo memberInfo = typeof(SomeClass); object[] attributes = memberInfo.GetCustomAttributes(true); foreach (object attribute in attributes) { CustomAttribute customAttribute = attribute as CustomAttribute; if (customAttribute != null) Console.WriteLine("Text = {0}", customAttribute.Text); else Console.WriteLine(); } } } [CustomAttribute("Hello World...")] public class SomeClass { } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] public class CustomAttribute : Attribute { private string text; public CustomAttribute(string text) { this.Text = text; } public string Text { get => text; set => text = value; } }
More high quality dry goods: see my GitHub: dotnetfly