private static void PrintResult(IHasName result) { Console.ForegroundColor = ConsoleColor.Blue; var resultType = result.GetType(); Console.WriteLine("Тип результату: " + resultType + "\nІм'я: " + result.Name); }
public static IHasName Couple(Human x, Human y) { Console.WriteLine($"Type #1: {x.GetType().Name}, Type #2: {y.GetType().Name}."); if (x.Sex == y.Sex) { throw new SexException("Equals = true.\n"); } CoupleAttribute xCouple = x.GetCoupleAttribute().SingleOrDefault(temp => temp.Pair == y.GetType().Name); CoupleAttribute yCouple = y.GetCoupleAttribute().SingleOrDefault(temp => temp.Pair == x.GetType().Name); bool xAcces = xCouple?.Probability >= random.NextDouble(); bool yAcces = yCouple?.Probability >= random.NextDouble(); if (xAcces && yAcces) { try { IHasName res = (IHasName)Activator.CreateInstance(Type.GetType(x.GetType().Namespace + "." + xCouple.ChildType)); var stringMethod = y.GetType().GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public).FirstOrDefault(temp => temp.ReturnType == typeof(string)); string name = (string)stringMethod?.Invoke(y, null); var nameProp = res.GetType().GetProperty("Name"); var surnameProp = res.GetType().GetProperty("Surname"); var sexProp = res.GetType().GetProperty("Sex"); nameProp.SetValue(res, name); if (surnameProp != null) { string surName = x.Sex == Sex.Men ? x.Name : y.Name; surName += (Sex)sexProp.GetValue(res) == Sex.Men ? "ович" : "овна"; surnameProp.SetValue(res, surName); } return(res); }catch (Exception e) { Console.WriteLine(e.Message); } } return(null); }
private static void SetSurname(IHasName child, string name) { var parName = child.GetType().GetProperty("Surname"); if (parName != null && parName.CanWrite) { parName.SetValue(child, name); } }
private void SetPatronymicIfExists(IHasName target, Human first, Human second) { var patronymicProperty = target.GetType().GetProperties(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).FirstOrDefault(x => x.Name == PatronymicPropertyName); if (patronymicProperty != null) { var s = NamesHelper.GeneratePatronimyc((first.Gender == Gender.Male ? first : second).Name, Gender.Female); patronymicProperty.SetValue(target, s); } }
static void Main(string[] args) { ConsoleKeyInfo con = new ConsoleKeyInfo(); Human h1, h2; do { try { h1 = Human.CreateHuman(); h2 = Human.CreateHuman(); IHasName res = Human.Couple(h1, h2); if (res != null) { string surname = ""; var surnameProp = res.GetType().GetProperty("Surname"); if (surnameProp != null) { surname = (string)surnameProp.GetValue(res); } Console.WriteLine($"{res.GetType().Name}. {res.Name}. {surname}\n"); } else { Console.WriteLine("Bad!\n"); } }catch (SexException e) { Console.WriteLine(e.Message); } con = Console.ReadKey(); } while (con.Key != ConsoleKey.Q && con.Key != ConsoleKey.F10); }