using System; using System.Linq; using System.Reflection; namespace listascs { /* 1st Assignment Author: F. Raszeja Original delivery date: 25/08/2020 Translated to English: 30/08/2021 Notes: Some of the proposed activities have too many variables so I was lazy and used an array, as it also has extra functions that help me a little bit, like Reverse(), etc. */ class Lista1 { static void exercicio1() { string[] info = new string[3]; Console.WriteLine("Name:"); info[0] = Console.ReadLine(); Console.WriteLine("Age:"); info[1] = Console.ReadLine(); Console.WriteLine("City:"); info[2] = Console.ReadLine(); Console.WriteLine("Name: {0}, Age: {1}, City: {2}", info[0], info[1], info[2]); } static void exercicio2() { int num; Console.WriteLine("Write a number, then press ENTER:"); num = int.Parse(Console.ReadLine()); Console.WriteLine("Number: {0}, Double: {1}", num, num*2); } static void exercicio3() { int num; Console.WriteLine("Write a number, then press ENTER:"); num = int.Parse(Console.ReadLine()); Console.WriteLine("Number: {0}, Number ^ 2: {1}",num,Math.Pow(num,2)); } static void exercicio4() { float[] nums = new float[3]; Console.WriteLine("Insert the values for A, B and C:"); for(int i = 0; i= val[2]) && (val[3] <= val[1])) ? true : false; bool res2 = ((val[1] == val[0]) && (val[2] == val[3])) ? true : false; Console.WriteLine(res || res2); } static bool ParseStr(string val) { if (!val.Any(x=> char.IsLetter(x) || x.Equals('0')) // using Linq to check if it has any 0's or if it's a letter. && !System.Text.RegularExpressions.Regex.IsMatch(val, @"^[1-9]{2,}$")){return true;} return false; } static void Main(string[] args) // UI Code here. { string sel = ""; do{ // Cleaning the console up before anything Console.Clear(); Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.Black; Console.WriteLine("Select one of the examples"); Console.ResetColor(); Console.WriteLine("\n1-9\tExample 1-9"); Console.WriteLine("\texit\tClose this program"); Console.Write("--> "); sel = Console.ReadLine(); if(sel != "exit" && ParseStr(sel)){ Type typesPrograma = typeof(Lista1); MethodInfo aInvocar = typesPrograma.GetMethod( "exercicio"+sel, BindingFlags.Static | BindingFlags.NonPublic); aInvocar.Invoke(null, null); /* typeof - get the object type of something, in this case, something from the "Program" class. MethodInfo - information related to the invoked method, in this case, we'll be using GetMethod to look up the methods with the name "exercicio" and the number (sel), e.g. exercicio1, exercicio2 Since I'm using only static methods, I have to filter it out using the binding flags. Then it's easy, just invoke the "aInvocar" MethodInfo, it shouldn't have any parameters or submethods so we'll give it null instead. */ } Console.WriteLine("\nPress any key to Continue..."); Console.ReadKey(); } while(sel.ToLower() != "exit"); } } }