473,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Get a reference to an object's base type

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
7 5734
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Agent Mulder | last post by:
Hi group, I try to get a reference to a template. In the template, a virtual function is declared. Later in the program, I inherit from classes created by the template. So the template is the...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
5
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and...
1
by: Mercede | last post by:
Hi, I've a certain problem. I've created a Class Library project that contains the following - A Factory Class - A base Object A factory can create the Base objects or objects derived from...
9
by: Dennis | last post by:
When a class (myownclass) inheirits another class, how can I get an object reference to the underlyng MyBase class instance from within myownclass. The base class has a method that I want to...
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
0
by: Richard Gregory | last post by:
Hi, I have the wsdl below, for an Axis web service, and when I select Add Web Refernce in Visual Studio the proxy is missing a class representing the returnedElementsType (see reference.cs below...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.