Advertisement
pinionszki

Atrybut

Sep 9th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Zad2
  8. {
  9.     [AttributeUsage(AttributeTargets.Class)]
  10.     class WazneAttribute:Attribute
  11.     {
  12.         bool ważność;
  13.  
  14.         public WazneAttribute(bool ważność)
  15.         {
  16.             this.ważność = ważność;
  17.         }
  18.  
  19.         public bool Ważność
  20.         {
  21.             get { return ważność; }
  22.         }
  23.     }
  24.  
  25.     [Wazne(true)]
  26.     class Zadanie
  27.     {
  28.         public string nazwa;
  29.         public string opis;
  30.  
  31.         public Zadanie(string nazwa, string opis)
  32.         {
  33.             this.opis = opis;
  34.             this.nazwa = nazwa;
  35.         }
  36.  
  37.         public override string ToString()
  38.         {
  39.             return $"{nazwa} to {opis}";
  40.         }
  41.     }
  42.  
  43.     [Wazne(false)]
  44.     class Termin
  45.     {
  46.         string opis;
  47.         DateTime data;
  48.         public Termin(string opis, DateTime data)
  49.         {
  50.             this.opis = opis;
  51.             data = new DateTime();
  52.         }
  53.  
  54.         public override string ToString()
  55.         {
  56.             return $"{data} to {opis}";
  57.         }
  58.     }
  59.    
  60.     class Program
  61.     {
  62.         static void Podaj(object o)
  63.         {
  64.             Type p = o.GetType();
  65.             object[] Attr = Attribute.GetCustomAttributes(p);
  66.             foreach (object item in Attr)
  67.             {
  68.                 if (item is WazneAttribute)
  69.                 {
  70.                     if ((((WazneAttribute)item).Ważność) == true)
  71.                     {
  72.                         Console.WriteLine(o.ToString()+" to wsm ważne");
  73.                     }
  74.                     else
  75.                     {
  76.                         Console.WriteLine(o.ToString()+" wywalone");
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.         static void Main(string[] args)
  82.         {
  83.             Zadanie z1 = new Zadanie("dupa", "pysior");
  84.             Termin t1 = new Termin("mecz", DateTime.Now);
  85.  
  86.             Podaj(z1);
  87.             Podaj(t1);
  88.             Console.ReadKey();
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement