Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Dynamic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Reflection;
  12.  
  13. namespace lab4_delegaty_interfejsy
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void button1_Click(object sender, EventArgs e)
  23.         {
  24.             int[] tablica = new int[4];
  25.             tablica.Sum();
  26.  
  27.             int a = 10;
  28.             int b = a.MojeRozszerzenie();
  29.             MessageBox.Show(b.ToString());
  30.         }
  31.  
  32.         private void button2_Click(object sender, EventArgs e)
  33.         {
  34.             //delkaracja obiektu klasy aonimowej
  35.             //kompilator rozpoznaje wlaściwości na podstawie wprowadznych danych
  36.             //nie można go zadeklarować globalnie
  37.             //nie trzeba tworzyć klasy żeby przechować kila właściwości
  38.             //spełniona zasada hermetyzacji
  39.             var obiektKlasyanonimowej = new { parametr1 = 50, info = "informacja", stan_obiektu = true, parametr2 = 13.21 };
  40.             //PD:
  41.             //jak pobrać wlaściwości obiektu innego typu i dodanie do obiektu var
  42.             //zrobić metode przeciążoną obiekatmi typu z którego pobieramy właściwości i...
  43.             MessageBox.Show(obiektKlasyanonimowej.parametr2.ToString());
  44.             //można operować na tych właściwościach wżej:
  45.             int c = obiektKlasyanonimowej.parametr1.MojeRozszerzenie();
  46.         }
  47.  
  48.         // dodawanie jednego pola
  49.         private object addProperties(object sourceObject, object varObject, string propertyName)
  50.         {
  51.             PropertyInfo property = sourceObject.GetType().GetProperty(propertyName);
  52.             PropertyInfo[] varObjectProperites = varObject.GetType().GetProperties();
  53.  
  54.             IDictionary<string, object> result = new Dictionary<string, object>();
  55.             foreach (PropertyInfo info in varObjectProperites)
  56.             {
  57.                 result.Add(info.Name, info.GetValue(varObject));
  58.             }
  59.  
  60.             result.Add(property.Name, property.GetValue(sourceObject));
  61.  
  62.             return result;
  63.         }
  64.  
  65.         // dodawanie tablicy wybranych pól
  66.         private object addProperties(object sourceObject, object varObject, string[] propertyNames)
  67.         {
  68.             var type = sourceObject.GetType();
  69.             PropertyInfo[] varObjectProperites = varObject.GetType().GetProperties();
  70.  
  71.             IDictionary<string, object> result = new Dictionary<string, object>();
  72.             foreach (string name in propertyNames)
  73.             {
  74.                 var property = type.GetProperty(name);
  75.                 result.Add(property.Name, property.GetValue(sourceObject));
  76.             }
  77.  
  78.             foreach (PropertyInfo info in varObjectProperites)
  79.             {
  80.                 result.Add(info.Name, info.GetValue(varObject));
  81.             }
  82.  
  83.             return result;
  84.         }
  85.        
  86.         // dodawanie wszystkich pól
  87.         private object addProperties(object sourceObject, object varObject)
  88.         {
  89.             PropertyInfo[] properties = sourceObject.GetType().GetProperties();
  90.             PropertyInfo[] varObjectProperites = varObject.GetType().GetProperties();
  91.  
  92.             IDictionary<string, object> result = new Dictionary<string, object>();
  93.             foreach(PropertyInfo info in properties)
  94.             {
  95.                 result.Add(info.Name, info.GetValue(sourceObject));
  96.             }
  97.  
  98.             foreach(PropertyInfo info in varObjectProperites)
  99.             {
  100.                 result.Add(info.Name, info.GetValue(varObject));
  101.             }
  102.  
  103.             return result;
  104.         }
  105.  
  106.         //deklaracja delegaty
  107.         delegate int Delegata(int x, int y); //zaleca się zeby pierwsza litera nazwy byla D
  108.  
  109.         private void button3_Click(object sender, EventArgs e)
  110.         {
  111.             Jakasklasa obiekt = new Jakasklasa();
  112.             Delegata del = new Delegata(obiekt.dzialanie); //parametrem konstruktora musi być metoda
  113.             MessageBox.Show(del(5, 10).ToString());
  114.         }
  115.  
  116.         private void button4_Click(object sender, EventArgs e)
  117.         {
  118.             Maszyna maszyna = new Maszyna();
  119.             WywolajLancuchMetod wywolaj = new WywolajLancuchMetod(maszyna);
  120.             wywolaj.Rozpocznij();
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement