Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace zad._2
- {
- [AttributeUsage(AttributeTargets.Method)]
- class DekorujAttribute : Attribute
- {
- string metoda;
- public DekorujAttribute(string metoda)
- {
- this.metoda = metoda;
- }
- public string Metoda
- {
- get { return metoda; }
- }
- }
- class Pracownik
- {
- string imie, nazwisko;
- double staz;
- public Pracownik(string imie, string nazwisko, double staz)
- {
- this.imie = imie;
- this.nazwisko = nazwisko;
- this.staz = staz;
- }
- [Dekoruj("ToString")]
- public override string ToString()
- {
- return imie + " " + nazwisko + ": " + staz;
- }
- }
- class Programista : Pracownik
- {
- string obowiazki;
- public Programista(string imie, string nazwisko, double staz, string obowiazki)
- : base(imie, nazwisko, staz)
- {
- this.obowiazki = obowiazki;
- }
- [Dekoruj("ToString")]
- public override string ToString()
- {
- return base.ToString() + " " + obowiazki;
- }
- }
- class Program
- {
- public static string Wyswietl(object o)
- {
- if (o is Pracownik)
- {
- Type typ = typeof(Pracownik);
- MethodInfo[] mi = typ.GetMethods();
- foreach (MethodInfo minfo in mi)
- {
- var atrybuty = minfo.GetCustomAttributes(typeof(DekorujAttribute), false);
- foreach (DekorujAttribute item in atrybuty)
- {
- if (item.Metoda == "ToString")
- {
- return "\"" + o.ToString() + "\"";
- }
- else return "Dana klasa nie posiada metody dekorowanej";
- }
- }
- }
- return "Dany typ nie jest pracownikiem";
- }
- static void Main(string[] args)
- {
- Pracownik p = new Pracownik("Andrzej", "Łapczyński", 12);
- Pracownik k = new Programista("Krzysztof", "Karaluch", 12, "Zjadacz kału");
- Console.WriteLine(Wyswietl(p));
- Console.WriteLine(Wyswietl(k));
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement