Afnan Bashir
Afnan Bashir

Reputation: 7419

Cannot extract attribute data

I am trying to extract attribute. Attribute are custom attributes. enter image description here But some how i am not able to use object.id thing.
For example my commented code adpter.id is not valid, even you see i have converted that object to its type.

Here is the code for the attribute:

 [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;
    }


}

Upvotes: 1

Views: 96

Answers (3)

Xharze
Xharze

Reputation: 2733

The default visibility for fields is private, try to make id and Title public.
You could also change the fields to properties with a private setter like this:

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

Or to readonly properties:

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

As it is considered bad design to create public fields.

Upvotes: 4

Muhammad Umer Asif
Muhammad Umer Asif

Reputation: 255

I guess given a class's default behavior as private members, you can try to change it to public!

Upvotes: 1

mortb
mortb

Reputation: 9859

Do you want to read id from another class? You are missing the public modifier.

public readonly int id; 

Variables, properties and methods missing modifier will automatically become private.

Hope this helps!

Upvotes: 1

Related Questions