Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Validator;
- namespace hetfo_gyakorlas
- {
- class Program
- {
- static void Main(string[] args)
- {
- foreach (var item in Loader2())
- {
- Console.WriteLine($"{item.Szolgaltato} {item.Netsebesseg}");
- }
- Console.ReadLine();
- }
- static List<Person> Loader()
- {
- List<Person> people = new List<Person>();
- int counter = -2;
- for (int i = 0; i < 10; i++)
- {
- Person person = new Person($"Pista{i}", counter);
- if (ValidatorClass.Validation(person))
- {
- people.Add(person);
- }
- counter++;
- }
- return people;
- }
- static List<Internet> Loader2()
- {
- List<Internet> people = new List<Internet>();
- int counter = -2;
- for (int i = 0; i < 10; i++)
- {
- Internet person = new Internet($"Digi{i}", counter);
- if (ValidatorClass.Validation(person))
- {
- people.Add(person);
- }
- counter++;
- }
- return people;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Validator;
- namespace hetfo_gyakorlas
- {
- class Internet
- {
- public Internet(string szolgaltato, int netsebesseg)
- {
- Szolgaltato = szolgaltato;
- Netsebesseg = netsebesseg;
- }
- [NotNull]
- public string Szolgaltato { get; set; }
- [NotNull]
- [NotNegative]
- public int Netsebesseg { get; set; }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Validator;
- namespace hetfo_gyakorlas
- {
- class Person
- {
- public Person(string name, int age)
- {
- Name = name;
- Age = age;
- }
- [NotNull]
- public string Name { get; set; }
- [NotNull]
- [NotNegative]
- public int Age { get; set; }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Reflection;
- namespace Validator
- {
- public static class ValidatorClass
- {
- public static bool Validation(object o)
- {
- Type type = o.GetType();
- PropertyInfo[] properties = type.GetProperties();
- bool ok = true;
- foreach (var prop in properties)
- {
- var data = prop.GetValue(o);//o object adott property erteke
- var attr = prop.GetCustomAttributes();//attributes of property
- ok = true;
- foreach (var atrrItem in attr)
- {
- if (atrrItem.GetType() == typeof(NotNull))
- {
- if (!ValidationMethods.NotNull(prop.GetValue(o)))
- {
- ok = false;
- }
- }
- if (atrrItem.GetType() == typeof(NotNegative))
- {
- if (!ValidationMethods.NotNegative(prop.GetValue(o)))
- {
- ok = false; ;
- }
- }
- }
- }
- return ok;
- }
- }
- public static class ValidationMethods
- {
- public static bool NotNull(object o)
- {
- return o != null;
- }
- public static bool NotNegative(object o)
- {
- return (int)o >= 0;
- }
- }
- public class NotNull : Attribute { }
- public class NotNegative : Attribute { }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement