L
L
leremin2018-10-11 21:54:59
C++ / C#
leremin, 2018-10-11 21:54:59

How to access the type of the base class?

class A {}
class B:A {}
var a = new A();
var b = new B();
      
Console.WriteLine(a.GetType());
Console.WriteLine(b.GetType());
Console.WriteLine((b as A).GetType());

Requires the last output to output "A". The example is very abbreviated, but the question is precisely this: I have an object b and type A, and I need to pull off such a feint. That is, for b to be defined as A. I don't know how else to describe it.
There, in fact, b is transferred to someone else's Dictionary, whose ideology I cannot change. Moreover, I do not work with types, but I pass objects to methods; dictionary - inside someone else's code. That is, I pass b, but I want this foreign dictionary to think that this is an object of the base type A. It occurred to me only to remove inheritance, then I would not have to "replace" the type, but I would like to "replace" it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VoidVolker, 2018-10-12
@VoidVolker

b.GetType().BaseType

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question