课程: C# Practice: Attributes

Solution: Attribute usage - C#教程

课程: C# Practice: Attributes

Solution: Attribute usage

- [Instructor] In my solution, you can see my class defined on line 18, the test info attribute class. I'm modifying or restricting the set of targets by using the attribute usage attribute shown on line 17. The first parameter to this attribute is attributestarget.method, ord together with attributetargets.class. So that restricts it to only those two targets. And of course I can add as many targets as I'd like by oring it together with other items from this enumeration. After the comma you can see a name parameter called allow multiple. I've set that to true. So let's see what that means. Do my code. I'm going to go down here. And you can see that I'm able to apply the test info on my class. You can see I'm also applying it to my update value method. Here you can see I'm applying it multiple times on the validate check sum method. Now let's go see if I restricted this correctly. I'll copy this and then I'll go down to my property, paste that in. And you see I'm getting a red squiggle that tells me this attribute test info is not valid on this declaration type. It is only valid on class or method declarations.

内容