Subclass System.Attribute : Attribute : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » Attribute »

 

Subclass System.Attribute








    
 
using System;

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

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

    public string Company
    {
        get { return company; }
        set { company = value; }
    }

    public string Name
    {
        get { return name; }
    }
}


[assembly: Author("Tom", Company = "Ltd.")]
[Author("Tom", Company = "Abc Ltd.")]
class SomeClass { }

[Author("Lena")]
public class SomeOtherClass
{
}


[Author("FirstName")]
[Author("Jack", 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);
        }
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» Attribute