By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

Get a reference to an object's base type

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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]

"Santi" <no*********************@gmail.com> wrote in message
news:cm**********@nsnmpen3-gest.nuria.telefonica-data.net...
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.

Nov 16 '05 #2

P: n/a
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

Santi wrote:
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.

Nov 16 '05 #3

P: n/a
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?
Nov 16 '05 #4

P: n/a
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]

"Santi" <no*********************@gmail.com> wrote in message
news:cm**********@nsnmrro2-gest.nuria.telefonica-data.net...
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?

Nov 16 '05 #5

P: n/a
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.
Nov 16 '05 #6

P: n/a
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.

"Santi" <no*********************@gmail.com> escribió en el mensaje
news:cm**********@nsnmrro2-gest.nuria.telefonica-data.net...
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.

Nov 16 '05 #7

P: n/a
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]

"Santi" <no*********************@gmail.com> wrote in message
news:cm**********@nsnmrro2-gest.nuria.telefonica-data.net...
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.

"Santi" <no*********************@gmail.com> escribió en el mensaje
news:cm**********@nsnmrro2-gest.nuria.telefonica-data.net...
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.


Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.