Create Attribute : Attribute Definition : Attribute C# Examples


C# Examples » Attribute » Attribute Definition »

 

Create Attribute









    
using  System;

[AttributeUsage(AttributeTargets.Class  |  AttributeTargets.Assembly,  AllowMultiple  =  true,  Inherited  =  false)]
public  class  AuthorAttribute  :  System.Attribute
{
        public  string  Company;  
        public  string  Name;        

        public  AuthorAttribute(string  name)
        {
                this.Name  =  name;
                Company  =  "";
        }
}


[Author("Name1")]
[Author("Name2",  Company  =  "Ltd.")]
class  MainClass
{
        public  static  void  Main()
        {
                Type  type  =  typeof(MainClass);

                object[]  attrs  =  type.GetCustomAttributes(typeof(AuthorAttribute),  true);

                foreach  (AuthorAttribute  a  in  attrs)
                {
                        Console.WriteLine(a.Name  +  ",  "  +  a.Company);
                }
        }
}
    
   
  
   



Output

Name2, Ltd.
Name1,


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Attribute
» Attribute Definition