Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Validator;
  7. namespace hetfo_gyakorlas
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13.  
  14.  
  15.  
  16. foreach (var item in Loader2())
  17. {
  18. Console.WriteLine($"{item.Szolgaltato} {item.Netsebesseg}");
  19. }
  20. Console.ReadLine();
  21.  
  22.  
  23.  
  24. }
  25. static List<Person> Loader()
  26. {
  27. List<Person> people = new List<Person>();
  28. int counter = -2;
  29. for (int i = 0; i < 10; i++)
  30. {
  31. Person person = new Person($"Pista{i}", counter);
  32. if (ValidatorClass.Validation(person))
  33. {
  34. people.Add(person);
  35. }
  36. counter++;
  37.  
  38.  
  39. }
  40. return people;
  41. }
  42. static List<Internet> Loader2()
  43. {
  44. List<Internet> people = new List<Internet>();
  45. int counter = -2;
  46. for (int i = 0; i < 10; i++)
  47. {
  48. Internet person = new Internet($"Digi{i}", counter);
  49. if (ValidatorClass.Validation(person))
  50. {
  51. people.Add(person);
  52. }
  53. counter++;
  54.  
  55.  
  56. }
  57. return people;
  58. }
  59.  
  60.  
  61.  
  62.  
  63. }
  64. }
  65.  
  66. using System;
  67. using System.Collections.Generic;
  68. using System.Linq;
  69. using System.Text;
  70. using System.Threading.Tasks;
  71. using Validator;
  72. namespace hetfo_gyakorlas
  73. {
  74. class Internet
  75. {
  76. public Internet(string szolgaltato, int netsebesseg)
  77. {
  78. Szolgaltato = szolgaltato;
  79. Netsebesseg = netsebesseg;
  80. }
  81. [NotNull]
  82. public string Szolgaltato { get; set; }
  83. [NotNull]
  84. [NotNegative]
  85. public int Netsebesseg { get; set; }
  86. }
  87. }
  88. using System;
  89. using System.Collections.Generic;
  90. using System.Linq;
  91. using System.Text;
  92. using System.Threading.Tasks;
  93. using Validator;
  94. namespace hetfo_gyakorlas
  95. {
  96. class Person
  97. {
  98. public Person(string name, int age)
  99. {
  100. Name = name;
  101. Age = age;
  102. }
  103.  
  104. [NotNull]
  105. public string Name { get; set; }
  106. [NotNull]
  107. [NotNegative]
  108. public int Age { get; set; }
  109.  
  110.  
  111. }
  112. }
  113. using System;
  114. using System.Collections.Generic;
  115. using System.Linq;
  116. using System.Text;
  117. using System.Threading.Tasks;
  118. using System.Reflection;
  119. namespace Validator
  120. {
  121. public static class ValidatorClass
  122. {
  123. public static bool Validation(object o)
  124. {
  125.  
  126. Type type = o.GetType();
  127. PropertyInfo[] properties = type.GetProperties();
  128. bool ok = true;
  129.  
  130.  
  131. foreach (var prop in properties)
  132. {
  133. var data = prop.GetValue(o);//o object adott property erteke
  134. var attr = prop.GetCustomAttributes();//attributes of property
  135.  
  136. ok = true;
  137. foreach (var atrrItem in attr)
  138. {
  139. if (atrrItem.GetType() == typeof(NotNull))
  140. {
  141.  
  142. if (!ValidationMethods.NotNull(prop.GetValue(o)))
  143. {
  144. ok = false;
  145.  
  146. }
  147.  
  148. }
  149. if (atrrItem.GetType() == typeof(NotNegative))
  150. {
  151.  
  152. if (!ValidationMethods.NotNegative(prop.GetValue(o)))
  153. {
  154. ok = false; ;
  155.  
  156. }
  157.  
  158. }
  159.  
  160. }
  161. }
  162. return ok;
  163.  
  164. }
  165.  
  166.  
  167. }
  168. public static class ValidationMethods
  169. {
  170. public static bool NotNull(object o)
  171. {
  172. return o != null;
  173. }
  174. public static bool NotNegative(object o)
  175. {
  176.  
  177. return (int)o >= 0;
  178. }
  179. }
  180.  
  181. public class NotNull : Attribute { }
  182. public class NotNegative : Attribute { }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement