2017-02-17 23 views
0

Googleにこれにどのようなフレーズを使用するかわかりません。[MyAttribute(Name = value)]の "Name = value"とは何か

[MyAttribute(MyOption=true,OtherOption=false)] 

Name=value部分は何ですか?

は、この属性を検討しますかそして、それを自分のカスタム属性でどのように実装できますか?しかし()の代わり{}と、

[AttributeUsage(AttributeTargets.All)] 
public class MyAttribute : Attribute 
{ 
    public string TestValue { get; set; } 
} 

[My(TestValue = "Hello World!")] 
public class MyClass{} 

だからそれはほとんどのオブジェクト初期化子の構文のように動作します:

+0

Display(Name = "Value")? – Hemal

答えて

5

あなたが公開されたインスタンス(非静的)プロパティまたはフィールドを宣言することで、これを使用することができます。


あなたの属性のパラメータを持つコンストラクタを提供する場合は、あなたが最初のパラメータを渡す必要があります。詳細は

[AttributeUsage(AttributeTargets.All)] 
public class MyAttribute : Attribute 
{ 
    public string TestValue { get; set; } 

    public MyAttribute(int arg) 
    {} 
} 

[My(42, TestValue = "Hello World!")] 
public class MyClass{} 

this tutorialをお読みください。

+0

にあります。どうすれば '[MyAttribute(" foo "、TestValue =" Hello World! "]'? – Ross

+0

@Rossが私の答えを更新しました。 –

+0

非常にクリアなお礼 – Ross

3

C#の仕様17.2 Attribute specification

属性は、属性名と 位置名前付き引数のオプションのリストで構成されています。位置指定引数(ある場合) は、指定された引数の前に置かれます。位置指定引数は、 のattribute-argument-expressionで構成されます。名前付き引数は、 の後に等号とそれに続く 属性引数式で構成され、これらはともに単純割り当てと同じ規則である によって制約されます。名前付き引数の順序は、 ではありません。だからここ

[MyAttribute(MyOption=true,OtherOption=false)] 

あなたは2つの名前付き引数を持ってい。名前付き引数とは何ですか?ここでも、C#の仕様17.3.1 Compilation of an attribute

名前は T(属性型)の非静的読み書きパブリックフィールドまたはプロパティを識別しなければなりません。 Tにそのようなフィールドまたはプロパティがない場合、コンパイル時エラー が発生します。

わかりやすく、私は信じています。あなたはより多くの名前付き引数が必要な場合は

public class MyAttribute : Attribute 
{ 
    public bool MyOption { get; set; } 
    public bool OtherOption { get; set; } 
} 

- 他の非静的パブリック読み書きを追加:ゲッターとセッターまたは非静的パブリックフィールドを持つこれらの名前のいずれか非静的パブリックプロパティ(最も可能性が高いが)MyAttributeクラスで宣言しますあなたが使用する名前のプロパティまたは非静的パブリックフィールド。

public class MyAttribute : Attribute 
{ 
    public bool MyOption { get; set; } 
    public bool OtherOption { get; set; } 
    public int Answer { get; set; } 
    // public int Answer; <- another option 
} 

使用方法(順番は関係ありません):

[MyAttribute(MyOption=true, Answer=42, OtherOption=false)] 
2

属性のインスタンスを作成するときには、プロパティを指定しています。

属性には、コンストラクタパラメータとプロパティがあります。これはプロパティを設定しています。下のサンプルのようにあなたは、コンストラクタ引数とプロパティの名前の位置のコンストラクタ引数を、混在させることができることに注意してください:

using System; 
using System.Linq; 
using System.Reflection; 

[AttributeUsage(AttributeTargets.All)] 
public class DemoAttribute : Attribute 
{ 
    public string Property { get; set; } 
    public string Ctor1 { get; set; } 
    public string Ctor2 { get; set; } 
    public string Ctor3 { get; set; } 

    public DemoAttribute(string ctor1, 
         string ctor2 = "default2", 
         string ctor3 = "default3") 
    { 
     Ctor1 = ctor1; 
     Ctor2 = ctor2; 
     Ctor3 = ctor3; 
    } 
} 

[Demo("x", ctor3: "y", Property = "z")] 
public class Test 
{ 
    static void Main() 
    { 
     var attr = (DemoAttribute) typeof(Test).GetCustomAttributes(typeof(DemoAttribute)).First(); 
     Console.WriteLine($"Property: {attr.Property}"); 
     Console.WriteLine($"Ctor1: {attr.Ctor1}"); 
     Console.WriteLine($"Ctor2: {attr.Ctor2}"); 
     Console.WriteLine($"Ctor3: {attr.Ctor3}"); 
    } 
} 

という名前のコンストラクタ引数の:の違いに注意してください、とプロパティの割り当てのための=

このコードの出力は

Property: z 
Ctor1: x 
Ctor2: default2 
Ctor3: y 

です。これは、C#の仕様は、この場合には、「引数の名前」という名前のコンストラクタ引数とプロパティの両方を呼び出すことを残念だ:(

関連する問題