private static void ReadInputLines() { var harvestingFields = new HarvestingFields(); Type classType = harvestingFields.GetType(); string inputLine; while ((inputLine = Console.ReadLine()) != "HARVEST") { switch (inputLine) { case "private": PrintPrivateFields(classType); break; case "protected": PrintProtectedFields(classType); break; case "public": PrintPublicFields(classType); break; case "all": PrintAllFields(classType); break; } } }
public static void Main() { var input = Console.ReadLine(); var type = Type.GetType("P01_HarvestingFields.HarvestingFields"); HarvestingFields instance = (HarvestingFields)Activator.CreateInstance(type); var fields = type .GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); var sb = new StringBuilder(); while (input != "HARVEST") { if (input == "private") { foreach (var x in fields) { if (x.IsPrivate) { sb.AppendLine($"{x.Attributes.ToString().ToLower()} {x.FieldType.Name} {x.Name}"); } } } else if (input == "protected") { foreach (var x in fields) { if (x.IsFamily) { sb.AppendLine($"protected {x.FieldType.Name} {x.Name}"); } } } else if (input == "public") { foreach (var x in fields) { if (x.IsPublic) { sb.AppendLine($"{x.Attributes.ToString().ToLower()} {x.FieldType.Name} {x.Name}"); } } } else if (input == "all") { foreach (var x in fields) { sb.AppendLine($"{x.Attributes.ToString().ToLower()} {x.FieldType.Name} {x.Name}"); } } input = Console.ReadLine(); } Console.WriteLine(sb.ToString().Replace("family", "protected").Trim()); }
public static void Main() { string command; HarvestingFields hf = new HarvestingFields(); while ((command = Console.ReadLine()) != "HARVEST") { var modifier = command; hf.Harvest(modifier); } }
public static void Main() { Type type = Type.GetType("P01_HarvestingFields.HarvestingFields"); FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); HarvestingFields harvestingFields = new HarvestingFields(); string command; while ((command = Console.ReadLine()) != "HARVEST") { if (command == "public") { foreach (var field in fields.Where(x => x.IsPublic)) { Console.WriteLine("public " + field.FieldType.Name + " " + field.Name); } } else if (command == "private") { foreach (var field in fields.Where(x => x.IsPrivate)) { Console.WriteLine("private " + field.FieldType.Name + " " + field.Name); } } else if (command == "protected") { foreach (var field in fields.Where(x => x.IsFamily)) { Console.WriteLine("protected " + field.FieldType.Name + " " + field.Name); } } else if (command == "all") { foreach (var field in fields) { string atribute = field.Attributes.ToString().ToLower(); if (atribute == "family") { atribute = "protected"; } Console.WriteLine(atribute + " " + field.FieldType.Name + " " + field.Name); } } } }
public static void Main() { Type harvestingFieldsType = new HarvestingFields().GetType(); List <FieldInfo> harvestingFieldsFields = harvestingFieldsType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).ToList(); string input = string.Empty; List <string> requestedAccessModifiers = new List <string>(); while ((input = Console.ReadLine()) != "HARVEST") { requestedAccessModifiers.Add(input); } StringBuilder sb = new StringBuilder(); foreach (string requestedField in requestedAccessModifiers) { foreach (FieldInfo field in harvestingFieldsFields) { if (requestedField == "all") { if (field.IsPrivate) { sb.Append("private "); } else if (field.IsPublic) { sb.Append("public "); } else if (field.IsFamily) { sb.Append("protected "); } } else { if (field.IsPrivate && requestedField == "private") { sb.Append("private "); } else if (field.IsPublic && requestedField == "public") { sb.Append("public "); } else if (field.IsFamily && requestedField == "protected") { sb.Append("protected "); } else { continue; } } string fieldType = field.FieldType.ToString().Split(new char[] { '.' }).Last(); sb.Append(fieldType + " "); sb.AppendLine(field.Name); } } Console.WriteLine(sb.ToString().Trim()); }