Advertisement
Krissy

Attributes

Feb 23rd, 2013
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. //Create a [Version] attribute that can be applied to structures, classes, interfaces,
  2. //enumerations and methods and holds a version in the format major.minor (e.g. 2.11).
  3. //Apply the version attribute to a sample class and display its version at runtime.
  4.  
  5. using System;
  6.  
  7. [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Interface
  8.     | AttributeTargets.Enum | AttributeTargets.Method, AllowMultiple = false)]
  9.  
  10. public class VersionAttribute : System.Attribute
  11. {
  12.     public int Major { get; private set; }
  13.     public int Minor { get; private set; }
  14.  
  15.     public VersionAttribute(int major, int minor)
  16.     {
  17.         this.Major = major;
  18.         this.Minor = minor;
  19.     }
  20. }
  21. [VersionAttribute(4, 11)]
  22. class VersionDemo
  23. {
  24.     static void Main()
  25.     {
  26.         Type type = typeof(VersionDemo);
  27.         object[] versionAttributes = type.GetCustomAttributes(false);
  28.         foreach (VersionAttribute versionAttribute in versionAttributes)
  29.         {
  30.             Console.WriteLine("The version of the class VersionDemo is {0}.{1}",
  31.                 versionAttribute.Major, versionAttribute.Minor);
  32.         }
  33.         Console.WriteLine();
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement