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#

4

8 に答える 8

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

それは私のお気に入りの一つです。ビルド時にコンパイラ警告 (オプションでコンパイラ エラー) を引き起こすプロパティ/メソッドを古いものとしてマークすることができます。

于 2008-10-16T20:28:25.117 に答える
3

少しだけ...

同期、インライン化など:

[MethodImpl]

コンポーネント モデル:

[TypeDescriptor], [DisplayName], [Editor]

シリアライゼーション:

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

宣言的セキュリティ:

[PrincipalPermission]

すべてのCOMのもの...

于 2008-10-16T21:44:40.343 に答える
2

私は長い間c#属性の包括的なリストが欲しかったのですが、MSDNのドキュメントやどこにもリストが見つかりませんでした。これは彼らのドキュメントの弱点の1つだと思います。

xmlシリアル化からプロパティを除外する場合は、[XmlIgnore]を使用します。

于 2008-10-16T20:33:20.283 に答える
2
[Browsable]

is a favorite of mine. (MSDN)

于 2008-10-16T20:26:59.347 に答える
1

多言語 UI でDescriptionとを使用している場合は、リソースベースのバージョン ( から反映)が役立つ場合があります。CategorySystem.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);
    }
}

whereSRは適切な へのラッパーResourceManagerです。

于 2008-10-16T21:14:19.953 に答える
1

C# プロパティ属性

于 2008-10-16T20:27:41.827 に答える
0

私は列挙型でよく使用します。列挙型に「デフォルト」または「不明」の値があり、ドロップダウンのように必ずしもコントロールにバインドする必要はありませんか? カスタム属性を追加するか、既存の属性を使用して、表示可能/不可能なアイテムを表します。

私は、イベント ブローカーとポリシー インジェクションを備えたフレームワークで多くの作業を行っています。追加のメタデータでイベントを装飾したり、イベントを疎結合したりする場合、属性は非常に貴重です。

PostSharp ( http://www.postsharp.org/ )のようなかなり新しいツールがいくつかあり、属性内の動作をカプセル化するために使用できます。そのサイトにいくつかの良い例があります。これらのパターンを使用してコードを作成できるのは驚くべきことです。. .

于 2008-10-16T21:43:08.163 に答える
0

LocalizableListBindable は、カスタム コンポーネントの設計者にとって興味深いものになる可能性があります。

于 2008-10-16T21:01:14.847 に答える