1

属性を抽出しようとしています。属性はカスタム属性です。ここに画像の説明を入力 しかし、どのように私はobject.id物事を使用することができません。
たとえば、コメントされたコードadpter.idは有効ではありません。そのオブジェクトをその型に変換したことがわかります。

属性のコードは次のとおりです。

 [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
 public class Adapter : Attribute
 {
    // This class implements the Adapter Attribite
    readonly int id;
    readonly string Title;
    public string Comment { get; set; }

    private string[] Relation;

    public Adapter(int id, string Title,string []relations)
    {
       this.id = id;
       this.Title = Title;
       this.Relation = relations;
    }


}
4

3 に答える 3

4

フィールドのデフォルトの可視性は非公開です。作成idしてTitle公開してみてください。
次のようなプライベート セッターを使用して、フィールドをプロパティに変更することもできます。

public Id { get; private set; }
public Title { get; private set; }

またはプロパティを読み取り専用にするには:

public Id { get { return id; }  }
public Title { get { return title; } }

public フィールドを作成するのは悪い設計と見なされるためです。

于 2012-04-26T08:39:14.267 に答える
1

別のクラスから id を読み取りますか? public 修飾子がありません。

public readonly int id; 

修飾子がない変数、プロパティ、およびメソッドは、自動的に非公開になります。

お役に立てれば!

于 2012-04-26T08:38:45.447 に答える
1

publicプライベートメンバーとしてのクラスのデフォルトの動作を考えると、それを!に変更しようとすることができます。

于 2012-04-26T08:40:37.263 に答える