Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# class can not disguise to be another class because GetType method cannot be override

Tags:

c#

there is a statement in the CLR via C# saying in C#, one class cannot disguise to be another, because GetType is virutal and thus it cannot be override

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

I must missed something

if I hide the base GetType implementation then I can disguise my class to be another class, is that correct?

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

Following is the NO.4 answer from the possible duplicate, so My question is more on this. is this kind of disguise possible, if so, how can we say that we can prevent class type disguise in C# ? regardless of the GetType is virtual or not

While its true that you cannot override the object.GetType() method, you can use "new" to overload it completely, thereby spoofing another known type. This is interesting, however, I haven't figured out how to create an instance of the "Type" object from scratch, so the example below pretends to be another type.

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

After creating an instance of this, (new NotAString()).GetType(), will indeed return the type for a string.

share|edit|flag answered Mar 15 at 18:39

Dr Snooze 213 By almost anything that looks at GetType has an instance of object, or at the very least some base type that they control or can reason about. If you already have an instance of the most derived type then there is no need to call GetType on it. The point is as long as someone uses GetType on an object they can be sure it's the system's implementation, not any other custom definition. – Servy Mar 15 at 18:54 add comment

like image 700
zinking Avatar asked Jun 26 '13 03:06

zinking


People also ask

Is C still used in 2020?

C/C++ is still powering the world despite number of new high level programming languages. Most of the major software applications including Adobe, Google, Mozilla, Oracle are all written in C/C++. There is a complete article on list of best applications written in C/C++.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.


3 Answers

This question only has meaning when you define a context:
Disguise from whom/what?

There are several options:

  1. Disguising so that runtime will interpret it as a different type:
    Not possible using this method. Runtime does not even call GetType.

  2. Disguising so that some other library will interpret it as a different type:
    Not possible using this method. If the library takes B you can not pass your class X, if the library takes object it will not call your GetType as it is new, not override.

  3. Disguising so that a developer may think it is some other type when debugging:
    Possible (if he does not notice your trick). Why, though?

like image 155
Andrey Shchekin Avatar answered Oct 23 '22 07:10

Andrey Shchekin


GetType is not virtual, otherwise it is supposed to be overriden. It's extern.

If you want to hide an implementation of the base class, you can use new modifier.

There're various ways to know the type of an object instance. The following example shows how easy 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);
    }
}

output of TestMethod:

System.Int32

Pretended

In c#, most(note: not all of) types derives from object, that is, their ultimate base class is object. And because GetType is not overridable, the base.GetType() is always there. Any approach to know the type without invoking the fake implementation of GetType(), just exposes the real type.

like image 35
Ken Kin Avatar answered Oct 23 '22 06:10

Ken Kin


GetType method is not marked as virtual, therefore it cannot be overriden. Hiding the implementation of the GetType is possible by using new keyword, but base.GetType() would still return the type correctly and it cannot be manipulated by the developer.

like image 20
Alireza Maddah Avatar answered Oct 23 '22 06:10

Alireza Maddah