开发者

What .Net attributes do people apply to their code? [duplicate]

开发者 https://www.devze.com 2022-12-16 09:27 出处:网络
This question already has answers here: 开发者_StackOverflow社区 Closed 12 years ago. Possible Duplicate:
This question already has answers here: 开发者_StackOverflow社区 Closed 12 years ago.

Possible Duplicate:

Most Useful Attributes in C#

I always feel that I am missing functionality that can be gained in .Net by simply applying attributes to classes, methods, properties etc. It doesn't help that intellisense cannot display all appropriate attributes as they can normally be applied in a wide range of scenarios.

Here's a couple of the attributes I like to use:

[DebuggerHidden] - placing this over methods prevents the Visual Studio debugger from stepping in to code. This is useful if you have an event that continually fires and interupts your debugging.

[EditorBrowsable(EditorBrowsableState.Never)] - Hide a method from intellisense. I don't use this often, but it's handy when building reusable components and you want to hide some test or debug methods.

I'd like to see what others are using and what tips people have.


I just found this resources:

  • Most Useful Attributes in C#
  • 5 Very Useful C# Attributes

 

// The DebuggerDisplayAttribute can be a sweet shortcut to avoid expanding
// the object to get to the value of a given property when debugging. 
[DebuggerDisplay("ProductName = {ProductName},ProductSKU= {ProductSKU}")] 
public class Product 
{ 
    public string ProductName { get; set; } 
    public string ProductSKU { get; set; } 
}

// This attribute is great to skip through methods or properties 
// that only have getters and setters defined.
[DebuggerStepThrough()] 
public virtual int AddressId 
{ 
    get { return _AddressId;}     
    set 
    { 
        _AddressId = value;   
        OnPropertyChanged("AddressId"); 
    } 
}

// The method below is marked with the ObsoleteAttribute. 
// Any code that attempts to call this method will get a warning.
[Obsolete("Do not call this method.")]
private static void SomeDeprecatedMethod() { }

// similar to using a combination of the DebuggerHidden attribute, which hides
// the code from the debugger, and the DebuggerStepThrough attribute, which tells
// the debugger to step through, rather than into, the code it is applied to.
[DebuggerNonUserCode()]
private static void SomeInternalCode() { }


I usually use [Browsable(false)] and [Serializable].

[Browsable(false)] on property hides the property from PropertyGrid.

Shouldn't this be community-wiki?


I realy like the DebuggerDisplay:

[DebuggerDisplay("Count = {count}")]
class MyHashtable
{
    public int count = 4;
}

It will instruct VS what to display when hovering over the item.


We have a lot of CLS compliant code and some not, so it's clearly a plus for us:

[assembly:CLSCompliant(true)]
[CLSCompliant(true)]

This helps us a lot.


[DebuggerDisplay(....)]

to define what fields of a structure or class I want to see in the debugger display.


Sometimes BindableAttribute is nice to influence the binding behavior of components. Maybe it is helpful to fire up Reflector and search for "Attribute" and browse a little bit. It depends on your intention which ones are useful.


I used conditional attributes during the creation of a demo application.I made it as a full version and suppress some functionalities using these type of attributes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号