Guest User

Untitled

a guest
Nov 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using System.Linq;
  4.  
  5. namespace Lab6
  6. {
  7. public abstract class //Тут починається код що я писав
  8. {
  9. public float Weight { get; set; }
  10. public string Color { get; set; }
  11.  
  12. public Animal(string color, float weight)
  13. {
  14. this.Color = color;
  15. this.Weight = weight;
  16.  
  17. }
  18.  
  19. public abstract string MakeSound();
  20.  
  21. }
  22.  
  23.  
  24. public abstract class AnimalWithTail: Animal
  25. {
  26. public float TailLength { get; set; }
  27.  
  28. public AnimalWithTail(string color, float weight, float tailLength)
  29. :base(color, weight)
  30. {
  31. this.TailLength = tailLength;
  32. }
  33. }
  34.  
  35.  
  36. public class Cat : AnimalWithTail
  37. {
  38. public Cat(string color, float weigth, float tailLength)
  39. : base(color, weigth, tailLength)
  40. {
  41.  
  42. }
  43.  
  44. private string Purr()
  45. { return "purrrrrrrr"; }
  46.  
  47. private string Meow()
  48. { return "Meow";}
  49.  
  50. public override string MakeSound()
  51. {
  52. return Purr() + Meow();
  53.  
  54. }
  55.  
  56. public override string ToString()
  57. {
  58. return $"This is a Cat, Color = {Color}, Weight = {Weight}, TailLength = {TailLength}";
  59. }
  60. }
  61.  
  62. public class Dog : AnimalWithTail
  63. {
  64. public Dog(string color, float weigth, float tailLength)
  65. : base(color, weigth, tailLength)
  66. {
  67.  
  68. }
  69.  
  70. public override string MakeSound()
  71. { return "Woof"; }
  72.  
  73. public override string ToString()
  74. {
  75. return $"This is a Dog, Color = {Color}, Weight = {Weight}, TailLength = {TailLength}";
  76. }
  77. } //Тут він закінчується
  78.  
  79. public class Program
  80. {
  81. public static void Main(string[] args)
  82. {
  83. PrintClassInfo(typeof(Animal));
  84. PrintClassInfo(typeof(AnimalWithTail));
  85. PrintClassInfo(typeof(Cat));
  86. PrintClassInfo(typeof(Dog));
  87. Console.WriteLine(new Cat("Balck", 2, 1));
  88. Console.WriteLine(new Dog("Brown", 5, 2));
  89. }
  90.  
  91. private static void PrintClassInfo(Type type){
  92. var props = type.GetProperties();
  93. var fields = type.GetFields().Concat(type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)).ToArray();
  94. var constructors = type.GetConstructors();
  95. var methods = type.GetMethods();
  96.  
  97. var sortLambda =
  98. new Func<MemberInfo, MemberInfo, int>(
  99. (first, second) =>
  100. String.Compare(first.Name, second.Name, StringComparison.Ordinal));
  101. Array.Sort(props, sortLambda.Invoke);
  102. Array.Sort(fields, sortLambda.Invoke);
  103. Array.Sort(constructors, sortLambda.Invoke);
  104. Array.Sort(methods, sortLambda.Invoke);
  105. foreach (var propertyInfo in props)
  106. {
  107. Console.WriteLine(
  108. $"{propertyInfo.Name}, {propertyInfo.PropertyType}, {propertyInfo.CanRead}, {propertyInfo.CanWrite}");
  109. }
  110. foreach (var fieldInfo in fields)
  111. {
  112. Console.WriteLine(
  113. $"{fieldInfo.Name}, {fieldInfo.FieldType}, {fieldInfo.IsPrivate}, {fieldInfo.IsPublic}, {fieldInfo.IsStatic}, {fieldInfo.IsInitOnly}, {fieldInfo.IsLiteral}");
  114. }
  115. foreach (var constructorInfo in constructors)
  116. {
  117. Console.WriteLine(
  118. $"{constructorInfo.Name}, {constructorInfo.DeclaringType}, {constructorInfo.IsPublic}, {string.Join(", ", constructorInfo.GetParameters().ToArray<object>())}");
  119. }
  120. foreach (var methodInfo in methods)
  121. {
  122. Console.WriteLine(
  123. $"{methodInfo.Name}, {methodInfo.DeclaringType}, {methodInfo.IsPublic}, {string.Join(", ", methodInfo.GetParameters().ToArray<object>())}");
  124. }
  125. }
  126. }
  127. }
Add Comment
Please, Sign In to add comment