C # class cannot be masked as another class because the GetType method cannot be overridden

there is a message in the CLR through C # saying in C # one class cannot mask itself with another, because GetType is virutal and therefore it cannot be overridden

but I think in C # we can still hide the parent implementation of GetType.

I have to miss something

If I hide the underlying GetType implementation, then I can override my class to be another class, is that true?

The key here is not whether GetType is virutal or not, the question is whether we can override one class to be different in C #

Below is answer No. 4 from a possible duplicate, so my question is more about that. is such masking possible, if so, how can we say that we can prevent class type masking in C #? whether gettype is virtual or not

Although it is true that you cannot override the object.GetType () method, you can use the "new" method to completely overload it, thereby releasing a known type. This is interesting, however, I did not understand how to instantiate the Type object from scratch, so the example below pretends to be a different type.

public class NotAString { private string m_RealString = string.Empty; public new Type GetType() { return m_RealString.GetType(); } } 

After instantiating this (new NotAString ()) GetType () will actually return the type for the string.

share | edit | flag replied March 15 at 18:39

Dr Snooze 213 Almost everything that looks at GetType has an instance of the object, or at least some basic type, which they control or may cause roughly. If you already have an instance of the most derived type, then there is no need to call GetType on it. The fact is that someone uses GetType on the object, he can be sure that the system and not any other user-defined definition. - Service March 15 at 18:54 add a comment

+7
source share
3 answers

This question only makes sense when defining a context:
Hide from whom / what?

There are several options:

  • Disguise the runtime to interpret it as a different type:
    Impossible using this method. Runtime does not even call GetType .

  • Disguise for some other library to interpret it as a different type:
    Impossible using this method. If the library accepts B , you cannot pass your class X , if the library accepts object , it will not call your GetType , as it is new , not override .

  • Disguise it so that the developer might think that during debugging this could be a different type:
    Maybe (if he doesn't notice the trick). Why though?

+6
source

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.

+4
source

GetType method is not marked as virtual , so it cannot be overridden. Hiding the GetType implementation GetType possible using the new keyword, but base.GetType() will still return the type correctly, and the developer will not be able to handle it.

+2
source

All Articles