Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Reflection;
- namespace HelloWold;
- public class Osoba
- {
- // Pola
- private string imie;
- private int wiek;
- // Właściwości
- public string Nazwisko { get; set; }
- // Konstruktor
- public Osoba(string imie, string nazwisko, int wiek)
- {
- this.imie = imie;
- this.Nazwisko = nazwisko;
- this.wiek = wiek;
- }
- // Metody
- public void UstawWiek(int nowyWiek)
- {
- wiek = nowyWiek;
- }
- public string ZwrocPelneImie()
- {
- return $"{imie} {Nazwisko}";
- }
- public int ZwrocWiek()
- {
- return wiek;
- }
- }
- public static class ObjectReflector
- {
- public static List<string> GetFieldNames(Object obj) {
- List<string> temp = new List<string>();
- FieldInfo[] temp2 = obj.GetType().GetFields(BindingFlags.NonPublic);
- foreach (FieldInfo info in temp2) {
- temp.Add(info.Name);
- }
- return temp;
- }
- public static List<string> GetPropertyNames(Object obj) {
- List<string> temp = new List<string>();
- PropertyInfo[] temp2 = obj.GetType().GetProperties();
- foreach (PropertyInfo info in temp2) {
- temp.Add(info.Name);
- }
- return temp;
- }
- public static List<string> GetMethods(Object obj) {
- List<string> temp = new List<string>();
- MethodInfo[] temp2 = obj.GetType().GetMethods();
- foreach (MethodInfo info in temp2) {
- temp.Add(info.Name);
- }
- return temp;
- }
- }
- public static class ListHelper
- {
- public static void Wyswietl(this List<string> list)
- {
- foreach (var item in list)
- {
- Console.WriteLine(item);
- }
- }
- }
- public class Samochod
- {
- // Pola
- private string marka;
- private int rokProdukcji;
- // Właściwości
- public string Model { get; set; }
- // Konstruktor
- public Samochod(string marka, string model, int rokProdukcji)
- {
- this.marka = marka;
- this.Model = model;
- this.rokProdukcji = rokProdukcji;
- }
- // Metody
- public void UstawRokProdukcji(int nowyRokProdukcji)
- {
- rokProdukcji = nowyRokProdukcji;
- }
- public string ZwrocMarkeModel()
- {
- return $"{marka} {Model}";
- }
- public int ZwrocRokProdukcji()
- {
- return rokProdukcji;
- }
- }
- public static class Program
- {
- static void Main(string[] args)
- {
- Osoba o = new Osoba("Paweł", "Kowalski", 25);
- Console.WriteLine("Pola klasy Osoba:");
- ObjectReflector.GetFieldNames(o).Wyswietl();
- Console.WriteLine("Właściwości klasy Osoba:");
- ObjectReflector.GetPropertyNames(o).Wyswietl();
- Console.WriteLine("Metody klasy Osoba:");
- ObjectReflector.GetMethods(o).Wyswietl();
- Console.WriteLine();
- Samochod s = new Samochod("Izera", "Elektryczna", 2999);
- Console.WriteLine("Pola klasy Samochod:");
- ObjectReflector.GetFieldNames(s).Wyswietl();
- Console.WriteLine("Właściwości klasy Samochod:");
- ObjectReflector.GetPropertyNames(s).Wyswietl();
- Console.WriteLine("Metody klasy Samochod:");
- ObjectReflector.GetMethods(s).Wyswietl();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement