Questions tagged [attributeusage]

10 questions
18
votes
5 answers

Why would one want to use AttributeUsage AllowMultiple when creating attributes?

According to a book I'm reading, the AllowMultiple public property of AttributeUsage specifies: ...whether the target can have multiple instances of the attribute applied to it. Why would I want/not want to use this?
Scott Davies
  • 3,665
  • 6
  • 31
  • 39
9
votes
3 answers

C# AttributeUsage for Specific Class

Is it possible to have something like AttributeUsage to restrict the use of an attribute to a specific class (not just AttributeTargets.Class - that would be any class)?
Alex
  • 75,813
  • 86
  • 255
  • 348
8
votes
1 answer

Why does F# not allow multiple attributes where C# does?

The following code compiles in C#: [ContentType("text")] [ContentType("projection")] public class Class1 { } The following code in F# does not compile: [] [] type Class1() = class end The compile…
Johan
  • 660
  • 1
  • 6
  • 13
5
votes
1 answer

C#: AttributeUsage behaving strange on inherited Attributes - Is this intended behaviour?

I'm working with custom attributes, which again have a common base class. The AttributeUsage attribute is declared on the base attribute class, not on the concrete attribute classes. My attribute usage on the base class says AttributeTargets.Class,…
antifish
  • 51
  • 1
4
votes
2 answers

Client-side validation for custom ValidationAttribute with AttributeTargets.Class

Is it possible to implement client-site validation for custom ValidationAttribute, which is used in Class scope? For example my MaxLengthGlobal, which should assure global max limit for all input…
4
votes
1 answer

Custom Attribute - Set Attribute Usage for Only Private Members

I creating a custom attribute and I would like to the set the AttributeUsage (or maybe some other attribute in the attribute class) so that I my attribute can only be used in private methods, is that possible? Thanks in advance for the answers!
Hugo
  • 6,244
  • 8
  • 37
  • 43
1
vote
2 answers

Get attribute information from inheriting class, inside static function

I have a situation where I need to get the value on a property on an attribute (decorator) applied to a class. That class that is decorated, is inheriting from an abstract class. It is this abstract class that needs to get the attribute information,…
pnizzle
  • 6,243
  • 4
  • 52
  • 81
1
vote
1 answer

How to create attribute with class restrictions

In C# there is a attribute called AttributeUsage, if you want to set this attribute to a class it automatically detects if the class is derived from the Attribute class and if not it will throw an error. How can I create such restrictions? I want to…
Robert Wolf
  • 191
  • 1
  • 11
1
vote
2 answers

Attribute help with Routing, compiler error

I have created a custom attribute called RouteAttribute: [AttributeUsage(AttributeTargets.Property)] public class RouteAttribute : Attribute { public string Url { get; set; } public bool CheckPhysicalUrlAccess { get; set; } public…
jaekie
  • 2,283
  • 4
  • 30
  • 52
1
vote
1 answer

Creating custom attributes for methods that has specific signature

I created simple class has AttributeUsage attribute. When I tried to build I got error: Attribute 'AttributeUsage' is only valid on classes derived from System.Attribute. Then I made my class inherits from Attribute and everything is fine. If I…
Dilshod
  • 3,189
  • 3
  • 36
  • 67