Hi Vikas,

Please go thru this. It may Helps you to some extent.

1)Attributes
-->Attributes are a mechanism for adding metadata to the program
itself.
-->Attributes allow developers to add descriptions to classes,
properties, and methods at design time that can then be examined at
runtime
-->Attributes are inserted into the metadata and are visible through
ILDasm metadata-reading tool.
-->An Attribute is an object that represents data you want to
associate with an elements (class, methods…) in program.
--->The element to which you attach an attribute is referred to as the
target of that attribute.

Two Types:

--->Intrinsic attributes are supplied as part of the Common Language
Runtime (CLR), and they are integrated into .NET.  Ex: [webmethod] ,
[serializable]…
---->Custom attributes are attributes that developer creates.

2)Attributes can be applied to

Assembly        -  Applied to the assembly itself
Class           - Applied to instances of the class
Constructor     - Applied to a given constructor
Delegate        - Applied to the delegated method
Enum                    - Applied to an enumeration
Event           - Applied to an event
Interface       -Applied to an interface
Method          -Applied to a method
Module          - Applied to a single module
Property, Struct…….
All types

3)Attribute Decalration
Declaration:
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor ,AllowMultiple = true)]
    public class BugFixAttribute : System.Attribute { }


 -Attribute Usage is an attribute applied to attributes: a meta-
attribute
The first argument is a set of flags that indicate the target--in this
case, the class and     its constructor.
The second argument is a flag that indicates whether a given element
might receive   more than one such attribute

4)Attribute Construction
public class BugFixAttribute : System.Attribute {

public BugFixAttribute(int bugID, string programmer, string date)   //
Constructor
 {
  this.bugID = bugID;
  this.programmer = programmer;
  this.date = date;
}
public int BugID {           //Read-Only properties
 get { return bugID; }
 }
public string Comment { get { return comment; } set { comment =
value; } }
}

//Note :Every attribute must have at least one constructor

5) Attribute Association

Association:

[BugFix(121, "Pradeep", "01/03/05")]
[BugFix(107, "Kumar", "01/04/05", Comment = “In MyMath class")
public class MyMath
    {
   //Statements
    }

-The convention is to append the word Attribute to your attribute
name

[assembly: BugFix(121, "Pradeep", "01/03/05")]  //Attribute
association to assembly

Note:   Must place assembly attributes after all using statements and
before any code.
             Attributes are placed before the element and enclosed in [ ]

6) Attributes can be accesible through Reflection

Please find the below example and comments in the program.

Example:

Suppose ,If your team organizer wants to keep track of BUG FIXES
information that contains: Id, Who had fixed this bug?, Last modified
date of particular elements(Class,method...)..Total number of bug
fixes.... in other assemblies.....
In this scenario Attributes are useful.

In the below example I have Created custom attributes and accessing
information of attribute in the same assembly....
You can try for accessing other assembly associated attributes.



using System;
using System.Collections.Generic;
using System.Text;


namespace CA_Reflection_example1
{
    using System;
    using System.Reflection;
    using System.Diagnostics;
    using System.IO;


    // create custom attribute to be assigned to class members
    [AttributeUsage( AttributeTargets.Class |
        AttributeTargets.Method |
        AttributeTargets.Property,
        AllowMultiple = true)]

    public class BugFix : System.Attribute
    {
        // **private member data
        private int bugID;
        private string comment;
        private string date;
        private string programmer;

        // **attribute constructor for positional parameters
        public BugFix(int bugID, string programmer, string date)
        {
            this.bugID = bugID;
            this.programmer = programmer;
            this.date = date;
        }

        // Read-only properties
        public int BugID
        {
            get
            {
                return bugID;
            }
        }

        // property for named parameter
        public string Comment
        {
            get
            {
                return comment;
            }
            set
            {
                comment = value;
            }
        }


        public string Date
        {
            get
            {
                return date;
            }
        }


        public string Programmer
        {
            get
            {
                return programmer;
            }
        }


    }


    // ********* assign the attributes to the class ********


    [BugFix(121, "Pradeep", "01/04/05")]
    [BugFix(107, "Kumar", "01/03/05", Comment = "In Mymath class")]
    public class MyMath
    {
        //Assign attribute to methods

        [BugFix(100, "Pradeep", "04/04/04")]
        public double DoFunc1(double param1)
        {
            return param1 + DoFunc2(param1);
        }
        [BugFix(101, "Kumar", "05/05/05")]
        public double DoFunc2(double param1)
        {
            return param1 / 3;
        }
        public void Dofunc3()
        { }

    }
    [BugFix(101, "Kumar", "05/05/05")]
    public class mymath1 : MyMath
    { }

    public class Tester
    {
        public static void Main()
        {

            MyMath mm = new MyMath();
            Console.WriteLine("Calling DoFunc(9). Result: {0}",
mm.DoFunc1(9));



            // retrieve the custom attributes using Reflection


        System.Reflection.MemberInfo inf = typeof(mymath1);


            //Custome attributes for method
            //string Current_Directory =
Directory.GetCurrentDirectory();
            //Assembly a = Assembly.LoadFrom(Current_Directory +
@"\CA_Reflection_example1.exe");

            //Type t = a.GetType("CA_Reflection_example1.MyMath");
            //System.Reflection.MethodInfo inf =
t.GetMethod("DoFunc1");

        object[] attributes;
        attributes = inf.GetCustomAttributes(typeof(BugFix), true);

            //attributes =
inf.GetCustomAttributes(typeof(BugFixAttribute), false);

        foreach (Object attribute in attributes)
        {
            BugFix bfa = (BugFix)attribute;
            Console.WriteLine("\nBugID: {0}", bfa.BugID);
            Console.WriteLine("Programmer: {0}", bfa.Programmer);
            Console.WriteLine("Date: {0}", bfa.Date);
            Console.WriteLine("Comment: {0}", bfa.Comment);

            Console.ReadLine();

        }

            Console.ReadLine();
        }

    }
}

On Oct 18, 10:50 am, Cerebrus <[EMAIL PROTECTED]> wrote:
> I was about to hotly contest the assertion that it was C# (as opposed
> to the CLS used for VB as well), that introduced Attributes. Then I
> found where you got your statements from :
>
> http://www.csharphelp.com/archives3/archive558.html
>
> ... and I see that the statement was picked out of context.
>
> P.S.: How's the heel healing ?
>
> On Oct 18, 7:51 am, "achilles.4k" <[EMAIL PROTECTED]> wrote:
>
>
>
> > C# introduced attributes, which allow you to embed information right
> > into the C# source code. Attributes are placed in square brackets,
> > e.g. [MTAThread], above an Attribute Target (e.g. class, field,
> > method, etcetera).  C# Attributes can be used to mark fields, methods,
> > classes etcetera with markings that another program can interpret.
>
> > On Oct 16, 10:04 pm, "VIKAS GARG" <[EMAIL PROTECTED]> wrote:
>
> > > Please can anyone tell me what are attributes used for.I have  gone 
> > > through
> > > several tutorials
> > > but everywhere it is explained in terms of reflection
> > > that how can we retrieve information of attributes using reflection
>
> > > [System.Serializable]public class SampleClass
> > > {
> > >     // Objects of this type can be serialized.
>
> > > }
>
> > > Will this make class serializable here
>
> > > after declaring attributes how do we use them- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web 
Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/DotNetDevelopment

You may subscribe to group Feeds using a RSS Feed Reader to stay upto date 
using following url  

<a href="http://feeds.feedburner.com/DotNetDevelopment";> 
http://feeds.feedburner.com/DotNetDevelopment</a>
-~----------~----~----~----~------~----~------~--~---

Reply via email to