Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Create a [Version] attribute that can be applied to structures, classes, interfaces,
- //enumerations and methods and holds a version in the format major.minor (e.g. 2.11).
- //Apply the version attribute to a sample class and display its version at runtime.
- using System;
- [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Interface
- | AttributeTargets.Enum | AttributeTargets.Method, AllowMultiple = false)]
- public class VersionAttribute : System.Attribute
- {
- public int Major { get; private set; }
- public int Minor { get; private set; }
- public VersionAttribute(int major, int minor)
- {
- this.Major = major;
- this.Minor = minor;
- }
- }
- [VersionAttribute(4, 11)]
- class VersionDemo
- {
- static void Main()
- {
- Type type = typeof(VersionDemo);
- object[] versionAttributes = type.GetCustomAttributes(false);
- foreach (VersionAttribute versionAttribute in versionAttributes)
- {
- Console.WriteLine("The version of the class VersionDemo is {0}.{1}",
- versionAttribute.Major, versionAttribute.Minor);
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement