Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Dynamic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Reflection;
- namespace lab4_delegaty_interfejsy
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- int[] tablica = new int[4];
- tablica.Sum();
- int a = 10;
- int b = a.MojeRozszerzenie();
- MessageBox.Show(b.ToString());
- }
- private void button2_Click(object sender, EventArgs e)
- {
- //delkaracja obiektu klasy aonimowej
- //kompilator rozpoznaje wlaściwości na podstawie wprowadznych danych
- //nie można go zadeklarować globalnie
- //nie trzeba tworzyć klasy żeby przechować kila właściwości
- //spełniona zasada hermetyzacji
- var obiektKlasyanonimowej = new { parametr1 = 50, info = "informacja", stan_obiektu = true, parametr2 = 13.21 };
- //PD:
- //jak pobrać wlaściwości obiektu innego typu i dodanie do obiektu var
- //zrobić metode przeciążoną obiekatmi typu z którego pobieramy właściwości i...
- MessageBox.Show(obiektKlasyanonimowej.parametr2.ToString());
- //można operować na tych właściwościach wżej:
- int c = obiektKlasyanonimowej.parametr1.MojeRozszerzenie();
- }
- // dodawanie jednego pola
- private object addProperties(object sourceObject, object varObject, string propertyName)
- {
- PropertyInfo property = sourceObject.GetType().GetProperty(propertyName);
- PropertyInfo[] varObjectProperites = varObject.GetType().GetProperties();
- IDictionary<string, object> result = new Dictionary<string, object>();
- foreach (PropertyInfo info in varObjectProperites)
- {
- result.Add(info.Name, info.GetValue(varObject));
- }
- result.Add(property.Name, property.GetValue(sourceObject));
- return result;
- }
- // dodawanie tablicy wybranych pól
- private object addProperties(object sourceObject, object varObject, string[] propertyNames)
- {
- var type = sourceObject.GetType();
- PropertyInfo[] varObjectProperites = varObject.GetType().GetProperties();
- IDictionary<string, object> result = new Dictionary<string, object>();
- foreach (string name in propertyNames)
- {
- var property = type.GetProperty(name);
- result.Add(property.Name, property.GetValue(sourceObject));
- }
- foreach (PropertyInfo info in varObjectProperites)
- {
- result.Add(info.Name, info.GetValue(varObject));
- }
- return result;
- }
- // dodawanie wszystkich pól
- private object addProperties(object sourceObject, object varObject)
- {
- PropertyInfo[] properties = sourceObject.GetType().GetProperties();
- PropertyInfo[] varObjectProperites = varObject.GetType().GetProperties();
- IDictionary<string, object> result = new Dictionary<string, object>();
- foreach(PropertyInfo info in properties)
- {
- result.Add(info.Name, info.GetValue(sourceObject));
- }
- foreach(PropertyInfo info in varObjectProperites)
- {
- result.Add(info.Name, info.GetValue(varObject));
- }
- return result;
- }
- //deklaracja delegaty
- delegate int Delegata(int x, int y); //zaleca się zeby pierwsza litera nazwy byla D
- private void button3_Click(object sender, EventArgs e)
- {
- Jakasklasa obiekt = new Jakasklasa();
- Delegata del = new Delegata(obiekt.dzialanie); //parametrem konstruktora musi być metoda
- MessageBox.Show(del(5, 10).ToString());
- }
- private void button4_Click(object sender, EventArgs e)
- {
- Maszyna maszyna = new Maszyna();
- WywolajLancuchMetod wywolaj = new WywolajLancuchMetod(maszyna);
- wywolaj.Rozpocznij();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement