2010-01-16 24 views
11

可能重複:
Most Useful Attributes in C#什麼.Net屬性適用於他們的代碼?

我總覺得,我很想念,可以在.net中通過簡單地將屬性應用於類,方法,屬性等,並沒有什麼幫助來獲得功能該intellisense無法顯示所有適當的屬性,因爲它們通常可以在各種場景中應用。

這裏有一對夫婦,我喜歡使用的屬性:

[DebuggerHidden] - 將這種過度的方法可以防止在Visual Studio調試器步進的代碼。如果您遇到不斷觸發並中斷調試的事件,這非常有用。

[EditorBrowsable(EditorBrowsableState.Never)] - 從intellisense中隱藏方法。我不經常使用它,但在構建可重用組件時您很方便,並且您想隱藏一些測試或調試方法。

我想看看別人正在使用和人們有什麼提示。

+2

+1大問題,希望看到一些迴應。 – 2010-01-16 11:10:23

+1

我實際上會投票結束,因爲這不是一個真正的問題。答案是,「所有這些」。屬性不會被創建爲不可用。 – 2010-01-16 11:15:07

+2

dup? http://stackoverflow.com/questions/144833/most-useful-attributes-in-c – 2010-01-16 11:15:07

回答

4

我剛剛發現這個資源:

 

// 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() { } 
1

我通常使用[Browsable(false)][可序列化]

[Browsable(false)]屬性隱藏屬性PropertyGrid。

這不應該是社區維基?

+0

我是這個網站的新手,我沒有看過維基,現在就來看看。謝謝 – Nanook 2010-01-16 11:15:14

0
[DebuggerDisplay(....)] 

定義我想在調試器顯示中看到的結構或類的字段。

1

我真的喜歡DebuggerDisplay

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

它將指導VS懸停在項目時顯示什麼。

2

我們有很多的CLS兼容的代碼,有些沒有,所以這顯然對我們來說是一個加號:

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

這對我們幫助很大。

+2

哦和[ComVisibleAttribute(true)] – mjsabby 2010-01-16 11:29:43

0

有時BindableAttribute很好地影響組件的綁定行爲。 也許是有幫助的火起來反射器,搜索「屬性」,然後瀏覽一點點。這取決於你的意圖哪些是有用的。

0

我創建一個演示application.I使得它作爲一個完整版本的過程中使用的條件屬性 ,抑制使用這些類型的屬性的一些功能。

相關問題