ATTRIBUTES

Hi everyone,
Again something make me confused little bit about C# programming.

ATTRIBUTES

Ok, I understood the usage of predefined attributes in C# suc that serializable, obsolote,conditionally,..

However, I did not exactly understood the usage of Custom Attributes in C#. Why do programmers use Custom Attributes What is ithe main purpose of using our own attributes The reason for asking this is that I can not find any situation that I have to use(create) my own Attributes.

Any help will be appreciated...

Mert(JavaBoy)



Answer this question

ATTRIBUTES

  • neal.xia

    It's really very simple - the base class provides functionality which simplifies the code in the derived class. As it's working on a variable, an attribute is much easier than remembering to call a method somewhere other than where the member variable is declared.

    It's also the only time I've used custom attributes, I have no other examples :-)



  • Peca55

    One situation that springs to mind, on a web app I worked on, our base class for pages had the code so we could add a variable, and through an attribute, make the base class associate that variable with a value from the query string of the page. So, I would think as you need code to make an attribute do anything, the answer is often likely to involve a situation where you write a base class, and an attribute forces the derived class to treat it differently somehow.



  • KumarChinna

    Attributes are for giving information about a class as a whole. As opposed to properties, which given information about one instance of a class (an object). Attribute information is stored with the class's Type object.

    For example, in the system at my job, we has sample tables of the same form:

    ID, Name, Code, Description.

    We turn these into enums :

    enum TableName {Name1=ID1, Name2=ID2, etc }

    However, in that, we lose the code & description. So we added custom attributes:

    enum TableName {

    [MyDescription(description1)]
    [MyCode(code1)]
    Name1=ID1,

    [MyDescription(description2)]
    [MyCode(code2)]
    Name2=ID2,

    etc }

    This way, with a little bit of code using Reflection, we can get the code & description from just the enum value.



  • Coxis

    Again, thanks very much for your interest.
    I think that I kept the idea which is enough for me.

    In attributes,

    We give attributes such that names, descriptions, version no that specify the identity of that class or any class members. The main pupose of using them to give informations about that member or class to the users of that project in run time.
    However, in order to accomplish this, we must create a class which is derived by System.Attribute class and then create a constructor for it to take the descripton and also verison of that member or class. But while using this attributes, we must write some additional code so as to make the attributes appear on the console. So in my opinion, it costs too much time which is a crucial concept for efficiency of our projects.
    For instance,
    In the below link,
    http://www.csharphelp.com/archives/archive119.html

    The programmer defines a new class in order to make Attributes appear on the console which can have the potential for decreasing the efficiency, is not it


    Briefly, to give the user a little bit information about a snippet about the project, it is useless method because of the above reasons.

    Do you agree with me
    If not, please explain...

    Thanks,
  • Ajao

    Thanks for your reply but it sounds too confused to me.
    Would you mind giving some more concrete examples about Attributes



  • ATTRIBUTES