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
172
votes
8 answers

How enumerate all classes with custom class attribute?

Question based on MSDN example. Let's say we have some C# classes with HelpAttribute in standalone desktop application. Is it possible to enumerate all classes with such attribute? Does it make sense to recognize classes this way? Custom attribute…
tomash
  • 12,742
  • 15
  • 64
  • 81
165
votes
4 answers

How to create a custom attribute in C#

Can anyone please explain to me a very basic example of a custom attribute with code?
slash shogdhe
  • 3,943
  • 6
  • 27
  • 46
131
votes
9 answers

How do I read an attribute on a class at runtime?

I am trying to create a generic method that will read an attribute on a class and return that value at runtime. How do would I do this? Note: DomainName attribute is of class DomainNameAttribute. [DomainName("MyTable")] Public class MyClass :…
Zaffiro
  • 4,834
  • 5
  • 36
  • 47
127
votes
4 answers

ASP.NET MVC 4 Custom Authorize Attribute with Permission Codes (without roles)

I need to control the access to views based on users privilege levels (there are no roles, only privilege levels for CRUD operation levels assigned to users) in my MVC 4 application. As an example; below the AuthorizeUser will be my custom…
chatura
  • 4,097
  • 4
  • 19
  • 19
98
votes
8 answers

Can I add custom methods/attributes to built-in Python types?

For example—say I want to add a helloWorld() method to Python's dict type. Can I do this? JavaScript has a prototype object that behaves this way. Maybe it's bad design and I should subclass the dict object, but then it only works on the subclasses…
jedmao
  • 10,224
  • 11
  • 59
  • 65
66
votes
3 answers

Find methods that have custom attribute using reflection

I have a custom attribute: public class MenuItemAttribute : Attribute { } and a class with a few methods: public class HelloWorld { [MenuItemAttribute] public void Shout() { } [MenuItemAttribute] public void Cry() { …
stoic
  • 4,700
  • 13
  • 58
  • 88
65
votes
7 answers

Exclude property from serialization via custom attribute (json.net)

I need to be able to control how/whether certain properties on a class are serialized. The simplest case is [ScriptIgnore]. However, I only want these attributes to be honored for this one specific serialization situation I am working on - if other…
Rex M
  • 142,167
  • 33
  • 283
  • 313
64
votes
2 answers

jquery: get value of custom attribute

html5 supports the placeholder attribute on input[type=text] elements, but I need to handle non-compliant browsers. I know there are a thousand plugins out there for placeholder but I'd like to create the 1001st. I am able to get a handle on the…
bflemi3
  • 6,698
  • 20
  • 88
  • 155
60
votes
7 answers

How to get Custom Attribute values for enums?

I have an enum where each member has a custom attribute applied to it. How can I retrieve the value stored in each attribute? Right now I do this: var attributes = typeof ( EffectType ).GetCustomAttributes ( false ); foreach ( object attribute in…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
60
votes
4 answers

What AttributeTarget should I use for enum members?

I want to use my IsGPUBasedAttribute for enum members like this: public enum EffectType { [IsGPUBased(true)] PixelShader, [IsGPUBased(false)] Blur } but the compiler doesn't let me to use: [AttributeUsage (AttributeTargets.Enum,…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
55
votes
4 answers

Is it possible to have a delegate as attribute parameter?

Is it possible to have a delegate as the parameter of an attribute? Like this: public delegate IPropertySet ConnectionPropertiesDelegate(); public static class TestDelegate { public static IPropertySet GetConnection() { return new…
George Silva
  • 3,454
  • 10
  • 39
  • 64
54
votes
4 answers

What are the similarities and differences between Java Annotations and C# Attributes?

I have a Java library I'm considering porting to C#. The Java library makes extensive use of annotations (at both build time and run time.) I've never used C# attributes, but understand that they are the rough equivalent of Java annotations. If I…
Jared
  • 25,520
  • 24
  • 79
  • 114
54
votes
1 answer

How to pass custom component parameters in java and xml

When creating a custom component in android it is often asked how to create and pass through the attrs property to the constructor. It is often suggested that when creating a component in java that you simply use the default constructor, i.e. new…
Emile
  • 11,451
  • 5
  • 50
  • 63
53
votes
7 answers

Lambda expression in attribute constructor

I have created an Attribute class called RelatedPropertyAttribute: [AttributeUsage(AttributeTargets.Property)] public class RelatedPropertyAttribute: Attribute { public string RelatedProperty { get; private set; } public…
Dave New
  • 38,496
  • 59
  • 215
  • 394
48
votes
3 answers

An attribute argument must be a constant expression, ...- Create an attribute of type array

Here is my custom attribute and a class I'm using it on: [MethodAttribute(new []{new MethodAttributeMembers(), new MethodAttributeMembers()})] public class JN_Country { } public class MethodAttribute : Attribute { public…
Football-Is-My-Life
  • 1,407
  • 7
  • 18
  • 35
1
2 3
78 79