Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. namespace zad._2
  2. {
  3. [AttributeUsage(AttributeTargets.Method)]
  4. class DekorujAttribute : Attribute
  5. {
  6. string metoda;
  7.  
  8. public DekorujAttribute(string metoda)
  9. {
  10. this.metoda = metoda;
  11. }
  12.  
  13. public string Metoda
  14. {
  15. get { return metoda; }
  16. }
  17. }
  18.  
  19. class Pracownik
  20. {
  21. string imie, nazwisko;
  22. double staz;
  23.  
  24. public Pracownik(string imie, string nazwisko, double staz)
  25. {
  26. this.imie = imie;
  27. this.nazwisko = nazwisko;
  28. this.staz = staz;
  29. }
  30.  
  31. [Dekoruj("ToString")]
  32. public override string ToString()
  33. {
  34. return imie + " " + nazwisko + ": " + staz;
  35. }
  36. }
  37.  
  38. class Programista : Pracownik
  39. {
  40. string obowiazki;
  41.  
  42. public Programista(string imie, string nazwisko, double staz, string obowiazki)
  43. : base(imie, nazwisko, staz)
  44. {
  45. this.obowiazki = obowiazki;
  46. }
  47.  
  48. [Dekoruj("ToString")]
  49. public override string ToString()
  50. {
  51. return base.ToString() + " " + obowiazki;
  52. }
  53. }
  54.  
  55. class Program
  56. {
  57. public static string Wyswietl(object o)
  58. {
  59. if (o is Pracownik)
  60. {
  61. Type typ = typeof(Pracownik);
  62. MethodInfo[] mi = typ.GetMethods();
  63. foreach (MethodInfo minfo in mi)
  64. {
  65. var atrybuty = minfo.GetCustomAttributes(typeof(DekorujAttribute), false);
  66. foreach (DekorujAttribute item in atrybuty)
  67. {
  68. if (item.Metoda == "ToString")
  69. {
  70. return "\"" + o.ToString() + "\"";
  71. }
  72. else return "Dana klasa nie posiada metody dekorowanej";
  73. }
  74. }
  75. }
  76. return "Dany typ nie jest pracownikiem";
  77. }
  78. static void Main(string[] args)
  79. {
  80. Pracownik p = new Pracownik("Andrzej", "Łapczyński", 12);
  81. Pracownik k = new Programista("Krzysztof", "Karaluch", 12, "Zjadacz kału");
  82. Console.WriteLine(Wyswietl(p));
  83. Console.WriteLine(Wyswietl(k));
  84. Console.ReadKey();
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement