Advertisement
Guest User

Problem 11. Create Custom Class Attribute

a guest
Jul 25th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. namespace CustomClassAttribute
  2. {
  3.     using System;
  4.  
  5.     public class CustomClassAttributeMain
  6.     {
  7.         public static void Main()
  8.         {
  9.             string command = Console.ReadLine();
  10.             while (command != "END")
  11.             {
  12.                 Type type = typeof(Weapon);
  13.                 object[] attributes = type.GetCustomAttributes(false);
  14.                 foreach (CustomAttribute attribute in attributes)
  15.                 {
  16.                     ExecuteCommand(command, attribute);
  17.                 }
  18.  
  19.                 command = Console.ReadLine();
  20.             }
  21.         }
  22.  
  23.         private static void ExecuteCommand(string command, CustomAttribute attribute)
  24.         {
  25.             if (command == "Author")
  26.             {
  27.                 Console.WriteLine($"Author: {attribute.Author}");
  28.             }
  29.             else if (command == "Revision")
  30.             {
  31.                 Console.WriteLine($"Revision: {attribute.Revision}");
  32.             }
  33.             else if (command == "Description")
  34.             {
  35.                 Console.WriteLine($"Class description: {attribute.Description}");
  36.             }
  37.             else if (command == "Reviewers")
  38.             {
  39.                 Console.WriteLine($"Reviewers: {attribute.Reviewers}");
  40.             }
  41.         }
  42.     }
  43.  
  44.     [CustomAttribute("Pesho", 3, "Used for C# OOP Advanced Course - Enumerations and Attributes.", "Pesho, Svetlio")]
  45.     public class Weapon
  46.     {
  47.     }
  48.  
  49.     [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  50.     public class CustomAttribute : Attribute
  51.     {
  52.         public CustomAttribute(string author, int revision, string description, string reviewers)
  53.         {
  54.             this.Author = author;
  55.             this.Revision = revision;
  56.             this.Description = description;
  57.             this.Reviewers = reviewers;
  58.         }
  59.  
  60.         public string Author { get; }
  61.  
  62.         public int Revision { get; }
  63.  
  64.         public string Description { get; }
  65.  
  66.         public string Reviewers { get; }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement