Quiz
csharp | by: offbeatful
posted: Feb, 4th 2010 | jump to bottom
#region Classes using System; public interface IWord { void Print(); } public interface IWord2 : IWord { new void Print(); } public abstract class Base { protected static string msg = "send "; public Base() { Console.Write(this.GetString()); } static Base() { Console.Write("Never "); } public virtual void Print() { Console.Write("to "); } protected virtual string GetString() { return "llama "; } } public class Derived : Base, IWord { static Derived() { Console.Write(Derived.msg); } public new virtual void Print() { Console.Write("do "); } protected override string GetString() { return "a "; } } public sealed class MoreDerived : Derived, IWord { public override void Print() { Console.Write("mach"); } void IWord.Print() { Console.Write("a "); } protected override string GetString() { return "do "; } } public sealed class MoreDerived2 : Derived, IWord2 { static MoreDerived2() { Console.Write("ine"); } public new void Print() { Console.Write("job. "); } void IWord2.Print() { Console.Write("job."); } protected override string GetString() { return "'s "; } } public abstract class Unfinished : Base { protected new void Print() { Console.Write("camel "); } protected override string GetString() { return "human "; } } public class Finished : Unfinished { } // new Derived();Never send a // Finished fin = new Finished();//human // fin.Print();//to // MoreDerived moreder = new MoreDerived(); //do //// new Derived(); // moreder.Print();//mach // MoreDerived2 moreder2 = new MoreDerived2();//ine's // moreder2.Print(); #endregion class Program { private static TextWriter _consoleWriter; static List<Record> records = new List<Record>(); private class Record { public string Code { get; set; } public string Output { get; set; } public string RequiredVariable { get; set; } public string Instance { get; set; } public override string ToString() { return Code + " // " + Output; } } static void Main(string[] args) { Assembly callingAssembly = Assembly.GetCallingAssembly(); foreach (Type type in callingAssembly.GetTypes()) { if (!type.IsAbstract && type.IsPublic) { EvaluteType(type); } } AnalyzeAndDisplay(); } private static void AnalyzeAndDisplay() { StringBuilder result = new StringBuilder("Never send a human to do a machine's job."); StringCollection instances = new StringCollection(); foreach (Record r in records) { Console.WriteLine(r.ToString()); } Console.WriteLine(); Console.WriteLine("Result:"); while (result.Length != 0) { Record searchResult = null; foreach (Record r in records) { if (result.ToString().StartsWith(r.Output)) { if (!string.IsNullOrEmpty(r.Instance)) { searchResult = r; } if (!string.IsNullOrEmpty(r.RequiredVariable) && instances.Contains(r.RequiredVariable) && searchResult == null) { searchResult = r; } } } if (searchResult != null) { result.Remove(0, searchResult.Output.Length); Console.WriteLine(searchResult.ToString()); if (!string.IsNullOrEmpty(searchResult.Instance)) { instances.Add(searchResult.Instance); } } else { break; } } } #region Evaluations private static void EvaluteType(Type type) { //Set text writer for the console StringWriter sw = ChangeConsoleOut(); object instance = Activator.CreateInstance(type); records.Add(new Record() { Code = type.Name + " " + type.Name.ToLower() + " = new " + type.Name + "();", Output = GetResultAndRestoreConsole(sw),//Restore and get output. Instance = instance.GetType().Name.ToLower() }); EvaluteMethodPrint(instance); if (instance.GetType().BaseType != typeof(object)) EvaluteMethodPrintForDerived(instance, instance.GetType().BaseType); EvaluteMethodPrintForAllInterfaces(instance); } private static void EvaluteMethodPrintForAllInterfaces(object instance) { foreach (Type interf in instance.GetType().GetInterfaces()) { interf.GetType().GetMethod("Print"); MethodInfo method = interf.GetMethod("Print"); StringWriter sw = ChangeConsoleOut(); method.Invoke(instance, null); records.Add(new Record() { Code = "((" + interf.Name + ")" + instance.GetType().Name.ToLower() + ").Print();", Output = GetResultAndRestoreConsole(sw),//Restore and get output. RequiredVariable = instance.GetType().Name.ToLower() }); } } private static void EvaluteMethodPrintForDerived(object instance, Type baseType) { baseType.GetType().GetMethod("Print"); MethodInfo method = baseType.GetMethod("Print"); StringWriter sw = ChangeConsoleOut(); method.Invoke(instance, null); records.Add(new Record() { Code = "((" + baseType.Name + ")" + instance.GetType().Name.ToLower() + ").Print();", Output = GetResultAndRestoreConsole(sw), //Restore and get output. RequiredVariable = instance.GetType().Name.ToLower() }); if (baseType.BaseType != typeof(object)) EvaluteMethodPrintForDerived(instance, baseType.BaseType); } private static void EvaluteMethodPrint(object instance) { MethodInfo method = instance.GetType().GetMethod("Print"); //Set text writer for the console StringWriter sw = ChangeConsoleOut(); method.Invoke(instance, null); records.Add(new Record() { Code = instance.GetType().Name.ToLower() + ".Print();", Output = GetResultAndRestoreConsole(sw),//Restore and get output. RequiredVariable = instance.GetType().Name.ToLower() }); } #endregion #region ConsoleHelpers private static string GetResultAndRestoreConsole(StringWriter writer) { writer.Close(); string result = writer.ToString(); Console.SetOut(_consoleWriter); return result; } private static StringWriter ChangeConsoleOut() { _consoleWriter = Console.Out; StringWriter writer = new StringWriter(); Console.SetOut(writer); return writer; } #endregion }
576 views




