GetType not virtual, otherwise it must be overridden. It is from the outside.
If you want to hide the base class implementation, you can use the new modifier.
There are various ways to find out the type of instance of an object. The following example shows how easily this disguise can be debunked:
public static class TestClass { public static void TestMethod() { var x=new Pretended(); Console.WriteLine("{0}", x.GetType()); Console.WriteLine("{0}", (x as object).GetType()); } } public partial class Pretended { public new Type GetType() { return typeof(int); } }
TestMethod output:
System.Int32
pretended to be
In C #, most (note: not all) types come from object , i.e. their final base class is object . And since GetType not GetType , base.GetType() always present. Any approach to knowing a type without calling a fake implementation of GetType() just provides a real type.
Ken kin
source share