kvadrat4o

Harvester

Mar 28th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. namespace _01HarestingFields
  2. {
  3.     using System;
  4.     using System.Text;
  5.     using System.Reflection;
  6.     using System.Linq;
  7.  
  8.     class HarvestingFieldsTest
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var input = "";
  13.             string result = "";
  14.             string harvestingfields = "HarvestingFields";
  15.             StringBuilder sb = new StringBuilder();
  16.             bool takePrivate = false;
  17.             bool takeProtected = false;
  18.             bool takePublic = false;
  19.             while ((input = Console.ReadLine()) != "HARVEST")
  20.             {
  21.                 switch (input)
  22.                 {
  23.                     case "private":
  24.                         takePrivate = true;
  25.                         result = GetAllFields(harvestingfields,sb,takePrivate,takeProtected,takePublic);
  26.                         Console.WriteLine(result);
  27.                         break;
  28.                     case "public":
  29.                         takePublic = true;
  30.                         result = GetAllFields(harvestingfields, sb, takePrivate, takeProtected, takePublic);                        
  31.                         Console.WriteLine(result);
  32.                         break;
  33.                     case "protected":
  34.                         takeProtected = true;
  35.                         result = GetAllFields(harvestingfields, sb, takePrivate, takeProtected, takePublic);
  36.                         Console.WriteLine(result);
  37.                         break;
  38.                     case "all":
  39.                         result = GetAllFields(harvestingfields, sb, takePrivate, takeProtected, takePublic);
  40.                         Console.WriteLine(result);
  41.                         break;
  42.                     default:
  43.                         break;
  44.                 }
  45.  
  46.             }
  47.         }
  48.  
  49.         private static string GetAllFields(string harvestingfields, StringBuilder sb, bool takePrivate, bool takeProtected, bool takePublic)
  50.         {
  51.             var typeOfHarvester = typeof(HarvestingFields);
  52.             FieldInfo[] fields = typeOfHarvester.GetFields( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic );
  53.             if (takePrivate)
  54.             {
  55.                 fields = fields.Where(f => f.IsPrivate).ToArray();
  56.                 takePrivate = false;
  57.             }
  58.             else if (takeProtected)
  59.             {
  60.                 fields = fields.Where(f => f.IsFamily).ToArray();
  61.                 takeProtected = false;
  62.             }
  63.             else if (takePublic)
  64.             {
  65.                 fields = fields.Where(f => f.IsPublic).ToArray();
  66.                 takePublic = false;
  67.             }
  68.             foreach (var field in fields)
  69.             {
  70.                 var accessModifier = field.Attributes.ToString().ToLower();
  71.                 if (accessModifier.Equals("family"))
  72.                 {
  73.                     accessModifier = "protected";
  74.                 }
  75.                 sb.AppendLine($"{accessModifier} {field.FieldType.Name} {field.Name}");
  76.             }
  77.  
  78.             return sb.ToString().TrimEnd();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment