Sunday, May 27, 2012

Most Useful Attributes in C#


I know that attributes are extremely useful. There are some predefined ones such as [Browsable(false)] which allows you to hide properties in the properties tab. Here is a good question explaining attributes: .NET: What are attributes?



What are the predefined attributes (and their namespace) you actually use in your projects?


Source: Tips4all

27 comments:

  1. [DebuggerDisplay] can be really helpful to quickly see customized output of a Type when you mouse over the instance of the Type during debugging. example:

    [DebuggerDisplay("FirstName={FirstName}, LastName={LastName}")]
    class Customer
    {
    public string FirstName;
    public string LastName;
    }


    This is how it should look in the debugger:



    Also, it is worth mentioning that [WebMethod] attribute with CacheDuration property set can avoid unnecessary execution of the web service method.

    ReplyDelete
  2. System.Obsolete is one of the most useful attributes in the framework, in my opinion. The ability to raise a warning about code that should no longer be used is very useful. I love having a way to tell developers that something should no longer be used, as well as having a way to explain why and point to the better/new way of doing something.

    The Conditional attribute is pretty handy too for debug usage. It allows you to add methods in your code for debug purposes that won't get compiled when you build your solution for release.

    Then there are a lot of attributes specific to Web Controls that I find useful, but those are more specific and don't have any uses outside of the development of server controls from what I've found.

    ReplyDelete
  3. [Flags] is pretty handy. Syntactic sugar to be sure, but still rather nice.

    [Flags]
    enum SandwichStuff
    {
    Cheese = 1,
    Pickles = 2,
    Chips = 4,
    Ham = 8,
    Eggs = 16,
    PeanutButter = 32,
    Jam = 64
    };

    public Sandwich MakeSandwich(SandwichStuff stuff)
    {
    Console.WriteLine(stuff.ToString());
    // ...
    }

    // ...

    MakeSandwich(SandwichStuff.Cheese
    | SandwichStuff.Ham
    | SandwichStuff.PeanutButter);
    // produces console output: "Cheese, Ham, PeanutButter"




    Leppie points out something I hadn't realized, and which rather dampens my enthusiasm for this attribute: it does not instruct the compiler to allow bit combinations as valid values for enumeration variables, the compiler allows this for enumerations regardless. My C++ background showing through... sigh

    ReplyDelete
  4. I like [DebuggerStepThrough] from System.Diagnostics.

    It's very handy for avoiding stepping into those one-line do-nothing methods or properties (if you're forced to work in an early .Net without automatic properties). Put the attribute on a short method or the getter or setter of a property, and you'll fly right by even when hitting "step into" in the debugger.

    ReplyDelete
  5. For what it's worth, here's a list of all .NET attributes. There are several hundred.

    I don't know about anyone else but I have some serious RTFM to do!

    ~ William Riley-Land

    ReplyDelete
  6. My vote would be for [Conditional]

    [Conditional("DEBUG")]
    public void DebugOnlyFunction()
    {
    // your code here
    }


    You can use this to add a function with advanced debugging features; like Debug.Write, it is only called in debug builds, and so allows you to encapsulate complex debug logic outside the main flow of your program.

    ReplyDelete
  7. I always use the DisplayName, Description and DefaultValue attributes over public properties of my user controls, custom controls or any class I'll edit through a property grid. These tags are used by the .NET PropertyGrid to format the name, the description panel, and bolds values that are not set to the default values.

    [DisplayName("Error color")]
    [Description("The color used on nodes containing errors.")]
    [DefaultValue(Color.Red)]
    public Color ErrorColor
    {
    ...
    }


    I just wish Visual Studio's IntelliSense would take the Description attribute into account if no XML comment are found. It would avoid having to repeat the same sentence twice.

    ReplyDelete
  8. In Hofstadtian spirit, the [Attribute] attribute is very useful, since it's how you create your own attributes. I've used attributes instead of interfaces to implement plugin systems, add descriptions to Enums, simulate multiple dispatch and other tricks.

    ReplyDelete
  9. [Serializable] is used all the time for serializing and deserializing objects to and from external data sources such as xml or from a remote server. More about it here.

    ReplyDelete
  10. I've found [DefaultValue] to be quite useful.

    ReplyDelete
  11. Here is the post about interesting attribute InternalsVisibleTo. Basically what it does it mimics C++ friends access functionality. It comes very handy for unit testing.

    ReplyDelete
  12. I'd suggest [TestFixture] and [Test] - from the nUnit library.

    Unit tests in your code provide safety in refactoring and codified documentation.

    ReplyDelete
  13. [XmlIgnore]


    as this allows you to ignore (in any xml serialisation) 'parent' objects that would otherwise cause exceptions when saving.

    ReplyDelete
  14. I have been using the [DataObjectMethod] lately. It describes the method so you can use your class with the ObjectDataSource ( or other controls).

    [DataObjectMethod(DataObjectMethodType.Select)]
    [DataObjectMethod(DataObjectMethodType.Delete)]
    [DataObjectMethod(DataObjectMethodType.Update)]
    [DataObjectMethod(DataObjectMethodType.Insert)]


    More info

    ReplyDelete
  15. If I were to do a code coverage crawl, I think these two would be top:

    [Serializable]
    [WebMethod]

    ReplyDelete
  16. DesignerSerializationVisibilityAttribute is very useful. When you put a runtime property on a control or component, and you don't want the designer to serialize it, you use it like this:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public Foo Bar {
    get { return baz; }
    set { baz = value; }
    }

    ReplyDelete
  17. Only a few attributes get compiler support, but one very interesting use of attributes is in AOP: PostSharp uses your bespoke attributes to inject IL into methods, allowing all manner of abilities... log/trace being trivial examples - but some other good examples are things like automatic INotifyPropertyChanged implementation (here).

    Some that occur and impact the compiler or runtime directly:


    [Conditional("FOO")] - calls to this method (including argument evaluation) only occur if the "FOO" symbol is defined during build
    [MethodImpl(...)] - used to indicate a few thing like synchronization, inlining
    [PrincipalPermission(...)] - used to inject security checks into the code automatically
    [TypeForwardedTo(...)] - used to move types between assemblies without rebuilding the callers


    For things that are checked manually via reflection - I'm a big fan of the System.ComponentModel attributes; things like [TypeDescriptionProvider(...)], [TypeConverter(...)], and [Editor(...)] which can completely change the behavior of types in data-binding scenarios (i.e. dynamic properties etc).

    ReplyDelete
  18. It's not well-named, not well-supported in the framework, and shouldn't require a parameter, but this attribute is a useful marker for immutable classes:

    [ImmutableObject(true)]

    ReplyDelete
  19. The attributes I use the most are the ones related to XML Serialization.

    XmlRoot

    XmlElement

    XmlAttribute

    etc...

    Extremely useful when doing any quick and dirty XML parsing or serializing.

    ReplyDelete
  20. [TypeConverter(typeof(ExpandableObjectConverter))]


    Tells the designer to expand the properties which are classes (of your control)

    [Obfuscation()]


    I think you can guess what this is for

    ReplyDelete
  21. In our current project, we use

    [ComVisible(false)]


    It controls accessibility of an individual managed type or member, or of all types within an assembly, to COM.

    More Info

    ReplyDelete
  22. I like using the [ThreadStatic] attribute in combination with thread and stack based programming. For example, if I want a value that I want to share with the rest of the rest of a call sequence, but I want to do it out of band (i.e. outside of the call parameters), I might employ something like this.

    class MyContextInformation : IDisposable {
    [ThreadStatic] private static MyContextInformation current;

    public MyContextInformation Current {
    get { return current; }
    }

    private MyContextInformation previous;


    public MyContextInformation(Object myData) {
    this.myData = myData;
    previous = current;
    current = this;
    }

    public void Dispose() {
    current = previous;
    }
    }


    Later in my code, I can use this to provide contextual information out of band to people downstream from my code. Example:

    using(new MyContextInformation(someInfoInContext)) {
    ...
    }


    The ThreadStatic attribute allows me to scope the call only to the thread in question avoiding the messy problem of data access across threads.

    ReplyDelete
  23. Being a middle tier developer I like

    System.ComponentModel.EditorBrowsableAttribute Allows me to hide properties so that the UI developer is not overwhelmed with properties that they don't need to see.

    System.ComponentModel.BindableAttribute Some things don't need to be databound. Again, lessens the work the UI developers need to do.

    I also like the DefaultValue that Lawrence Johnston mentioned.

    System.ComponentModel.BrowsableAttribute and the Flags are used regularly.

    I use
    System.STAThreadAttribute
    System.ThreadStaticAttribute
    when needed.

    By the way. I these are just as valuable for all the .Net framework developers.

    ReplyDelete
  24. In this question I asked about any attributes that can help the .Net runtime to better perform.

    ReplyDelete
  25. [System.Security.Permissions.PermissionSetAttribute] allows security actions for a PermissionSet to be applied to code using declarative security.

    // usage:
    public class FullConditionUITypeEditor : UITypeEditor
    {
    // The immediate caller is required to have been granted the FullTrust permission.
    [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
    public FullConditionUITypeEditor() { }
    }

    ReplyDelete
  26. // on configuration sections
    [ConfigurationProperty]

    // in asp.net
    [NotifyParentProperty(true)]

    ReplyDelete
  27. [DeploymentItem("myFile1.txt")]
    MSDN Doc on DeploymentItem

    This is really useful if you are testing against a file or using the file as input to your test.

    ReplyDelete