Advertisement
Eziox

Untitled

May 15th, 2024
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5.  
  6. namespace HelloWold;
  7.  
  8. public class Osoba
  9. {
  10.     // Pola
  11.     private string imie;
  12.     private int wiek;
  13.  
  14.     // Właściwości
  15.     public string Nazwisko { get; set; }
  16.  
  17.     // Konstruktor
  18.     public Osoba(string imie, string nazwisko, int wiek)
  19.     {
  20.         this.imie = imie;
  21.         this.Nazwisko = nazwisko;
  22.         this.wiek = wiek;
  23.     }
  24.  
  25.     // Metody
  26.     public void UstawWiek(int nowyWiek)
  27.     {
  28.         wiek = nowyWiek;
  29.     }
  30.  
  31.     public string ZwrocPelneImie()
  32.     {
  33.         return $"{imie} {Nazwisko}";
  34.     }
  35.  
  36.     public int ZwrocWiek()
  37.     {
  38.         return wiek;
  39.     }
  40. }
  41.  
  42.  
  43. public static class ObjectReflector
  44. {
  45.     public static List<string> GetFieldNames(Object obj) {
  46.         List<string> temp = new List<string>();
  47.         FieldInfo[] temp2 = obj.GetType().GetFields(BindingFlags.NonPublic);
  48.         foreach (FieldInfo info in temp2) {
  49.             temp.Add(info.Name);
  50.         }
  51.         return temp;
  52.     }
  53.     public static List<string> GetPropertyNames(Object obj) {
  54.         List<string> temp = new List<string>();
  55.         PropertyInfo[] temp2 = obj.GetType().GetProperties();
  56.         foreach (PropertyInfo info in temp2) {
  57.             temp.Add(info.Name);
  58.         }
  59.         return temp;
  60.     }
  61.     public static List<string> GetMethods(Object obj) {
  62.         List<string> temp = new List<string>();
  63.         MethodInfo[] temp2 = obj.GetType().GetMethods();
  64.         foreach (MethodInfo info in temp2) {
  65.             temp.Add(info.Name);
  66.         }
  67.         return temp;
  68.     }
  69. }
  70.  
  71. public static class ListHelper
  72. {
  73.     public static void Wyswietl(this List<string> list)
  74.     {
  75.         foreach (var item in list)
  76.         {
  77.             Console.WriteLine(item);
  78.         }
  79.     }
  80. }
  81.  
  82. public class Samochod
  83. {
  84.     // Pola
  85.     private string marka;
  86.     private int rokProdukcji;
  87.  
  88.     // Właściwości
  89.     public string Model { get; set; }
  90.  
  91.     // Konstruktor
  92.     public Samochod(string marka, string model, int rokProdukcji)
  93.     {
  94.         this.marka = marka;
  95.         this.Model = model;
  96.         this.rokProdukcji = rokProdukcji;
  97.     }
  98.  
  99.     // Metody
  100.     public void UstawRokProdukcji(int nowyRokProdukcji)
  101.     {
  102.         rokProdukcji = nowyRokProdukcji;
  103.     }
  104.  
  105.     public string ZwrocMarkeModel()
  106.     {
  107.         return $"{marka} {Model}";
  108.     }
  109.  
  110.     public int ZwrocRokProdukcji()
  111.     {
  112.         return rokProdukcji;
  113.     }
  114. }
  115.  
  116. public static class Program
  117. {
  118.     static void Main(string[] args)
  119.     {
  120.         Osoba o = new Osoba("Paweł", "Kowalski", 25);
  121.         Console.WriteLine("Pola klasy Osoba:");
  122.         ObjectReflector.GetFieldNames(o).Wyswietl();
  123.         Console.WriteLine("Właściwości klasy Osoba:");
  124.         ObjectReflector.GetPropertyNames(o).Wyswietl();
  125.         Console.WriteLine("Metody klasy Osoba:");
  126.         ObjectReflector.GetMethods(o).Wyswietl();
  127.  
  128.         Console.WriteLine();
  129.         Samochod s = new Samochod("Izera", "Elektryczna", 2999);
  130.         Console.WriteLine("Pola klasy Samochod:");
  131.         ObjectReflector.GetFieldNames(s).Wyswietl();
  132.         Console.WriteLine("Właściwości klasy Samochod:");
  133.         ObjectReflector.GetPropertyNames(s).Wyswietl();
  134.         Console.WriteLine("Metody klasy Samochod:");
  135.         ObjectReflector.GetMethods(s).Wyswietl();
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement