6

Possible Duplicate:
Most Useful Attributes in C#

besides:

[DefaultValue(100)]
[Description("Some descriptive field here")]
public int MyProperty{get; set;}

What other C# Attributes are useful for Properties, after learning these I feel like I'm Missing out.

Related Questions

Most Useful Attributes in C#

Community
  • 1
  • 1
maxfridbe
  • 3,221
  • 3
  • 23
  • 13
  • This is so close to be duplicated with the link Mitchel Sellers just put in the question. What next, useful attributes or class. enumeration, etc :P – Patrick Desjardins Oct 16 '08 at 21:16

8 Answers8

7
[Obsolete("This is an obsolete property")]

That's one of my favourites. Allows you to mark a property/method obsolete, which will cause a compiler warning (optionally, a compiler error) on build.

TheSmurf
  • 15,337
  • 3
  • 40
  • 48
  • +1 You almost say this, but this attribute is also useful as a note to the programmer during development. – Mark Hurd Jun 10 '14 at 14:47
3

Just a few...

synchronization, inlining, etc:

[MethodImpl]

component model:

[TypeDescriptor], [DisplayName], [Editor]

serialization:

[Serializable], [DataMember], [XmlElement], [XmlAttribute], [NonSerialized], etc

declarative security:

[PrincipalPermission]

all the COM stuff...

Michael Haren
  • 105,752
  • 40
  • 168
  • 205
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2
[Browsable]

is a favorite of mine. (MSDN)

Greg D
  • 43,259
  • 14
  • 84
  • 117
  • `[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]` is something I often use together with the BrowsableAttribute. – newman Nov 01 '11 at 18:45
2

I've wanted a comprehensive list of c# attributes for a long time, but have never found a list in MSDN docs or anywhere. I think this is one of the weaker parts for their documentation.

I use [XmlIgnore] if I want to exclude a property from xml serialization.

P a u l
  • 7,805
  • 15
  • 59
  • 92
  • 1
    The documentation for the "Attribute" class has them all because inhering classes are displayed ;) http://msdn.microsoft.com/en-us/library/system.attribute.aspx – FunctorSalad Feb 05 '10 at 09:33
1

C# property attributes

Community
  • 1
  • 1
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
1

If you are using the Description and Category in multi-lingual UIs, then you may find useful the resource-based versions (reflected from System.Windows.Forms):

[AttributeUsage(AttributeTargets.All)]
internal sealed class SRDescriptionAttribute : DescriptionAttribute
{
    private bool replaced;

    public SRDescriptionAttribute(string description) : base(description)
    {
    }

    public override string Description
    {
        get
        {
            if (!this.replaced)
            {
                this.replaced = true;
                base.DescriptionValue = SR.GetString(base.Description);
            }
            return base.Description;
        }
    }
}

[AttributeUsage(AttributeTargets.All)]
internal sealed class SRCategoryAttribute : CategoryAttribute
{
    public SRCategoryAttribute(string category) : base(category)
    {
    }

    protected override string GetLocalizedString(string value)
    {
        return SR.GetString(value);
    }
}

where SR is a wrapper to the appropriate ResourceManager.

Panos
  • 18,992
  • 6
  • 45
  • 54
0

Localizable as well as ListBindable may be interesting for custom component designers.

Matt H
  • 7,311
  • 5
  • 45
  • 54
0

I use it quite often on enumerations. Ever have that "default" or "unknown" value in an enum, but you don't necessarily want bound to a control, like a dropdown? Add a custom attribute, or use an existing one, to represent items that should/should not be viewable.

I do a lot of work with frameworks that have event brokers and policy injection, and attributes are invaluable when it comes to decorating events with extra metadata or loosely coupling events.

There are a few fairly new tools like PostSharp (http://www.postsharp.org/) you can use to encapsulate behavior inside attributes. Couple good examples on that site; it's amazing how much simpler you can make code through those patterns . . .

joshua.ewer
  • 3,944
  • 3
  • 25
  • 35