Advertisement
Gizmon

Untitled

Mar 30th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3.  
  4. namespace zadanie_113
  5. {
  6. class Przyklad
  7. {
  8. public int publika { get; set; }
  9. private int prywata { get; set; }
  10. public Przyklad()
  11. {
  12. publika = 5;
  13. prywata = 5;
  14. }
  15.  
  16. private int ten()
  17. {
  18. return 10;
  19. }
  20. }
  21.  
  22. class Program
  23. {
  24. static void Main(string[] args)
  25. {
  26. Przyklad p = new Przyklad();
  27. Type typ = typeof(Przyklad);
  28.  
  29. PropertyInfo priv = typ.GetProperty("prywata", BindingFlags.NonPublic | BindingFlags.Instance);
  30. Console.WriteLine("Wartość przed: {0}", priv.GetValue(p));
  31. priv.SetValue(p, 4);
  32. Console.WriteLine("Wartość po: {0}", priv.GetValue(p));
  33.  
  34. MethodInfo met = typ.GetMethod("ten", BindingFlags.NonPublic | BindingFlags.Instance);
  35. Console.WriteLine(met.Invoke(p, null));
  36.  
  37. PropertyInfo pub = typ.GetProperty("publika");
  38. for(int i = 0; i < 4; i++)
  39. pub.GetValue(p);
  40. DateTime Start = DateTime.Now;
  41. for(int i = 0; i < 1000000; i++)
  42. pub.GetValue(p);
  43. DateTime End = DateTime.Now;
  44. TimeSpan Time = End - Start;
  45. Console.WriteLine("Czas dla refleksji: {0}", Time);
  46.  
  47. int trial = p.publika;
  48. for(int i = 0; i < 4; i++)
  49. trial = p.publika;
  50. Start = DateTime.Now;
  51. for(int i = 0; i < 1000000; i++)
  52. trial = p.publika;
  53. End = DateTime.Now;
  54. Time = End - Start;
  55. Console.WriteLine("Czas dla normalnego sposobu: {0}", Time);
  56.  
  57. Console.ReadLine();
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement