Get a reference to an object's base type

S

Santi

I have two classes: Product and Fruit which inherits from Product.
If I try to narrow the reference to the base type by a cast, I always get a
reference to the inherited type.
For example:

Fruit lemon = new Fruit();
Product prod = (Product)lemon; // Now prod is a Fruit Type!!

I want to get a Product reference from a Fruit object, is it possible?
thank you.
 
T

Thomas P. Skinner [MVP]

A derived class inherits all public and protected members from the base
class. No cast is needed to access the members of the base class. Casting a
derived class to a base class type only serves to hide the members of the
derived class. The actual object's type will remain the same. Maybe it would
help to know what you are trying to do. In fact, for the example you gave
you don't even need that cast. Declaring prod as type Product is all you
need. You will not get a compiler error if you leave out the cast. Upcasts
can be implicit. Downcast must be explicit.

I would take a look at the sections in a good C# book that discuss casting
and the use of the is and as operators. This will help you understand things
better.

Thomas P. Skinner [MVP]
 
G

Glen

Hi Santi,

Using the BaseType property of the Type.GetType method will return a
reference of System.Type to the parent object:

Type productType = Type.GetType("Fruit").BaseType;

Hope this helps.

- Glen
 
S

Santi

Hi, thank you. The scenario is this:

I have a method that receives an object as a parameter. Depending of the
objects type it does something. If it is from an inherited type then two
times this method has to be called: one for the base type and another with
the inherited tipe.

So if the method detects that the object inherits from a known type then
calls itself again recursevely whith the object narrowed to the base type,
so the function will detect its base type and perform the operations needed
for that specific type, then when the call returns it will do what it is
needed for the inherited type.

well....that is how I planned it in my mind :)

The problem is that even if I dynamically create a new instance from the
base type with reflection, just when I assign it to the inherited refference
it "expands" and converts itself to the inherited type. I think that this is
because it is only a reference. but the question is: is it possible to get a
reference only to the object's base type part so when I ask its type I
receive the base type?
 
T

Thomas P. Skinner [MVP]

So as I see it you want to "fool" the CLR type system into thinking that an
object is a base type object. I don't think that is possible. I don't
understand why you can't just use the "is" operator to determine the type.
It will evaluate to false if the object is not a fruit object. Then you know
it is a base class object. I guess your problem stems from doing something
recursively and that's where your problem is. Why not keep track of the
recursion depth with an extra argument or some such? If you do that then you
can repeatedly upcast the object n times or some such.

Have you thought of using polymorphism to accomplish this? That seems like a
better approach to me.

Thomas P. Skinner [MVP]
 
S

Santi

Well, I think that this kind of casts are possible with unmanaged c++, so I
was guessing if they could also be done in C#
The problem with polymorphism is that I don't know in advance what the types
would be. What I am writing is a utility that deals with objects and an xml
file that provides their description, therefore everything should be
dynamical.

thank you.
 
S

Santi

I forgot to explain this:

I want to "extract" the base object because the approach that I have taken
is to add an extra argument with the type to the function.
This solves the problem but also creates a new one: if it receives an
inherited object and the base type as the second argument then
it will not be able to detect that it is an inherited type.

So I think that the best solution would be that the object carry it's own
type information... but maybe it is just not possible.
 
T

Thomas P. Skinner [MVP]

Well C++ doesn't have the typing capabilities that C# has so you can be
sloppy. Also remember that you can use pointers to break all the rules with
C++.

It should be easy to have the class type marked inside the class. I have
done this in C++ many times. Come up with a scheme, e.g., and enum for the
type. Have the constructor save the type in the field. You can then fool
your code by changing the type from derived class to base class.

Have fun

Thomas P. Skinner [MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top