//try overriding Equal, ==, !=, GetHashCode public override bool Equals(object obj) { pug temp = (pug)obj; if (temp.name == name) { return(true); } else { return(false); } }
public pug(pug i) : base(i) { snorer = i.snorer; }
public static void dogInheritance() { pug Emma = new pug("Emma", "fawn", 3, 2014, false); chihuahua Eagle = new chihuahua("Eagle", "fawn", 6, 2010, false); Emma.showInfo(); pug Emily = new pug(Emma); Emily.name = "Emily"; Console.WriteLine("Equals: " + Emily.Equals(Emma)); Console.WriteLine("Operator Equals: " + (Emily == Emma) + "\n"); Emily.showInfo(); Eagle.showInfo(); int i = 5; object a = i; Console.WriteLine(a); try { try { Emma.speak(new string[] { "hi", "my name is Emma.", "I am a talkative pug", "as you can see" }); Eagle.speak(new string[] { "hi", "my name is Eagle." }); // throw new DivideByZeroException(); //manually throw to catch } catch (dogException) // this will catch dogException { Console.WriteLine("error: dog talks too much"); //throw; //manually throw outter catch } finally { Console.WriteLine("finally"); } } catch { Console.WriteLine("got"); } //Runtime type id & reflection dog d = new dog(); d = Eagle as dog; chihuahua c = new chihuahua(); c = Eagle as chihuahua; Console.WriteLine(d.name); pug puggy = null; Type t_chi = typeof(pug); Console.WriteLine(t_chi.FullName); System.Reflection.ConstructorInfo[] cons = t_chi.GetConstructors(); for (int b = 0; b < cons.Length; b++) { System.Reflection.ParameterInfo[] pa = cons[b].GetParameters(); if (pa.Length == 5) { object[] consArgs = { "puggy", "fawn", 5, 2007, false }; puggy = (pug)cons[b].Invoke(consArgs); Console.WriteLine(puggy.name); break; } } System.Reflection.MethodInfo[] mi = t_chi.GetMethods(System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (System.Reflection.MethodInfo mi_info in mi) { System.Reflection.ParameterInfo[] pa_info = mi_info.GetParameters(); /*foreach (System.Reflection.ParameterInfo pa in pa_info) * { * Console.WriteLine("returnType: " + mi_info.ReturnType.Name + ", Method Name: " + mi_info.Name + ", Method Parameter:" + pa); * } */ if (mi_info.Name.Equals("speak", StringComparison.Ordinal) && pa_info[0].ParameterType == typeof(string[])) { object[] arg = new object[1]; arg[0] = new string[] { "my", "name" }; Console.WriteLine(); /* * for(int b = 0; b < ((string[])arg[0]).Length; b++) * Console.WriteLine(((string[])arg[0])[b]); */ Console.WriteLine(); try { mi_info.Invoke(puggy, arg); } catch (Exception exc) { Console.WriteLine(exc); } Type t = typeof(pug); Console.WriteLine(t.AssemblyQualifiedName); } Console.WriteLine("hihihdoihfapdhfap"); pug.writedown(); } }