Questions tagged [custom-attributes]

In .NET Framework, custom attributes are user defined attributes which enable providing metadata for application elements such as assemblies, classes and methods.

In .NET Framework, custom attributes are user defined attributes which enable providing metadata for application elements such as assemblies, classes and methods.

1173 questions
24
votes
7 answers

Is it possible to query custom Attributes in C# during compile time ( not run-time )

In other words could it be possible to create assembly, which does not even compile (assuming the checking code is not removed ) if each one of the Classes does not have ( "must have" ) custom attributes ( for example Author and Version ) ? Here is…
Yordan Georgiev
  • 5,114
  • 1
  • 56
  • 53
24
votes
6 answers

How to get a custom attribute from object instance in C#

Let's say I have a class called Test with one property called Title with a custom attribute: public class Test { [DatabaseField("title")] public string Title { get; set; } } And an extension method called DbField. I am wondering if getting…
kabucey
  • 2,418
  • 3
  • 18
  • 7
23
votes
3 answers

Attributes and Named/Optional constructor parameters not working

I have custom attribute defined like so: [AttributeUsage(AttributeTargets.Field)] public class EnumDisplayAttribute : Attribute { public string Description { get; private set; } public string Code { get; private set; } public…
Shiv Kumar
  • 9,599
  • 2
  • 36
  • 38
23
votes
4 answers

Using action parameters in custom Authorization Attribute in ASP.NET MVC3

I have a controller which should only request authorization when loaded with specific parameters. Like when the parameter ID is 8 for example. I came up with using a custom validation attribute like this: public class MyAuthorizeAttribute :…
vinczemarton
  • 7,756
  • 6
  • 54
  • 86
23
votes
1 answer

ASP.NET MVC 4 custom Authorize attribute - How to redirect unauthorized users to error page?

I'm using a custom authorize attribute to authorize users' access based on their permission levels. I need to redirect unauthorized users (eg. user tries to delete an invoice without Delete acess level) to access denied page. The custom attribute is…
chatura
  • 4,097
  • 4
  • 19
  • 19
22
votes
4 answers

How do I set custom HTML attributes in django forms?

I have a Django form that is part of page. Lets say I have a field: search_input = forms.CharField(_(u'Search word'), required=False) I can access it only in template via {{ form.search_input }}. How to set custom HTML attrs (such as name and…
JackLeo
  • 4,579
  • 9
  • 40
  • 66
22
votes
4 answers

Attribute.IsDefined doesn't see attributes applied with MetadataType class

If I apply attributes to a partial class via the MetadataType attribute, those attributes are not found via Attribute.IsDefined(). Anyone know why, or what I'm doing wrong? Below is a test project I created for this, but I'm really trying to apply…
shaunmartin
  • 3,849
  • 2
  • 25
  • 26
22
votes
4 answers

jQuery - Looping through elements with specific Attribute

I know how to loop through the inputs below, searching for the ones with a specific class of "testers" And here's how I do that:
coffeemonitor
  • 12,780
  • 34
  • 99
  • 149
21
votes
1 answer

Reflection with generic syntax fails on a return parameter of an overridden method

To avoid old-fashioned non-generic syntax when searching for attributes of a known type, one usually uses the extension methods in System.Reflection.CustomAttributeExtensions class (since .NET 4.5). However this appears to fail if you search for an…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
20
votes
1 answer

Generating a custom compile time warning C#

I'm using VS2008 and would like to create a compile time warning / error based on custom attributes on a property (if it is possible). There are two cases which interest me currently: [MyAttribute (typeof(MyClass)] Where MyClass has to implement an…
user164771
20
votes
10 answers

How to define named Parameters C#

This seems like a simple question, but for some reason I can't find the answer anywhere. Basically, I'd like to be able to implement a constructor that takes NamedParameters. By named parameters, I do not mean parameters with default values…
Pierluc SS
  • 3,138
  • 7
  • 31
  • 44
19
votes
1 answer

Custom attribute with dash in name using EditorFor/TextBoxFor/TextBox helpers

I am using Knockout-JS to bind properties in my view to my view model. Knockout-JS uses a custom attribute called 'data-bind' that you have to append to controls in which you want to be bound to view model objects. Example:
Michael Brennan
  • 1,031
  • 8
  • 19
19
votes
2 answers

How to add custom attributes to ASP.NET controls

I've got an ASP.NET control say checkbox: Is it possible to add this custom Value attribute from code-behind and respectively get the value from Value Something like…
Anton Belev
  • 11,963
  • 22
  • 70
  • 111
18
votes
6 answers

Is it a bad practice to add extra attributes to HTML elements?

Sometimes I add an attribute to some of my controls. Like: Chris Sharma I know it is not a valid html. But it helps me in some cases. Is this considered as a bad practice? A friend of mine says that it is…
pencilCake
  • 51,323
  • 85
  • 226
  • 363
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
1 2
3
78 79