0

Hi dudes,
Many times when I read others codes I found they're using attributes , and actually I don't know when to use attributes, or when it becomes necessary to use it to be aware of it in my written codes. So, would some one here help me.

Any help appreciated, thanks a lot guys.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by sknake
2

It helps organize or flag members of a class. If you're serializing a class you may want to mark the members you want to include in serialization with an attribute, or maybe your logic serializes all public members and you only want to ignore certain members with the XmlIgnore attribute. I wrote a data layer where I "bind" a class to an SQL Table with attributes:

[DBTableInfo("vea_Alarm")]
  public sealed class Alarm : SqlObject
  {
    [DBFieldInfo("AlarmId", SqlDbType.Int, PrimaryKey=true, Identity=true)]
    private int _alarmId = 0;
    [DBFieldInfo("EventId", SqlDbType.Int)]
    private int _eventId;
    [DBFieldInfo("ServerId", SqlDbType.Int)]
    private int _serverId;
    [DBFieldInfo("SensorId", SqlDbType.Int)]
    private int _sensorId;
    [DBFieldInfo("SensorRow", SqlDbType.Int)]
    private int _sensorRow;
    [DBFieldInfo("IOPointNum", SqlDbType.Int)]
    private int _IOPointNum;
    [DBFieldInfo("EventNum", SqlDbType.Int)]
    private int _eventNum;
    [DBFieldInfo("StartDate", SqlDbType.DateTime)]
    private DateTime _startDate;
    [DBFieldInfo("EndDate", SqlDbType.DateTime)]
    private DateTime _endDate;
    [DBFieldInfo("CorrectedDate", SqlDbType.DateTime)]
    private DateTime _correctedDate;
    [DBFieldInfo("State", SqlDbType.VarChar, Size=DataLength.State)]
    private string _state;
    [DBFieldInfo("Alarm", SqlDbType.Bit)]
    private bool _alarm;
    [DBFieldInfo("Closed", SqlDbType.Bit)]
    private bool _closed;
    [DBFieldInfo("Corrected", SqlDbType.Bit)]
    private bool _corrected;
    [DBFieldInfo("IOName", SqlDbType.VarChar, Size=DataLength.IOName)]
    private string _ioName;

The code automatically generates a Select, Insert, Update, and Delete queries based on the attributes it finds.

Maybe you want to control serialization:

[Serializable]
  public sealed class SeriesTag
  {
    public SeriesTag()
    {
      this.Identifier = string.Empty;
      this.TrendIndex = 0;
      this.DataSourceGUID = string.Empty;
      this.SeriesGUID = string.Empty;
    }
    public SeriesTag(Series Parent)
      : this()
    {
      this.Parent = Parent;
    }
    [XmlIgnore]
    public Series Parent { get; set; }

Its just a handy way of going about things at runtime without having to maintain lists. You can reflect members and look for attributes.

Votes + Comments
Very useful :)
everytime i read your posts i learn something new :p
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.