views:

2130

answers:

8

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#

+2  A: 
[Browsable]

is a favorite of mine. (MSDN)

Greg D
+1  A: 

C# property attributes

CMS
+6  A: 
[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.

DannySmurf
+2  A: 

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
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
A: 

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

Matt
A: 

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
A: 

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
+2  A: 

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...

Marc Gravell