473,387 Members | 1,863 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,387 software developers and data experts.

Dynamic typecast of boxed object

Hello there.

This should be simple, but im having trouble anyway of getting it to
work.
I have a boxed object that i want to cast to its native type, but I
would like to use reflection to do the typecasting.
This is the regular way.
object obj = new Foo();
.... some dynamic invokation
Foo real = (Foo)obj;
real.DoSomething()
I would like it to do like the following
object obj = new Foo();
....
Foo real = (cast to typeof obj.getType() ) obj;
real.DoSomething()
Is it possible to do this dynamicly?

Aug 2 '06 #1
8 4215
If you are able to find ANY method in the reflection API that has the
return type Foo, so you don't need to cast... I doubt it.

What do you need it for?

Cheers,
Stefan


ak*****@gmail.com schrieb:
Hello there.

This should be simple, but im having trouble anyway of getting it to
work.
I have a boxed object that i want to cast to its native type, but I
would like to use reflection to do the typecasting.
This is the regular way.
object obj = new Foo();
... some dynamic invokation
Foo real = (Foo)obj;
real.DoSomething()
I would like it to do like the following
object obj = new Foo();
...
Foo real = (cast to typeof obj.getType() ) obj;
real.DoSomething()
Is it possible to do this dynamicly?
Aug 2 '06 #2
I have many classes almost identical in pairs, but in different
namespaces. ( Interface dont work for this.)

It is a kind of copy constructor, Im traveling down the class and
coping the primitive types or change the class for there parallel
complex types.

This gives me a new object (class2) of the new type but with the other
classes data (class1).
but the new object is boxed (object) And i need it as its basetype. but
it has to be casted according to the object i put into the box from the
beginning.

Basicly im creating a class of a specific type, and filling it via
invokation of it.
Now i would like to assign this object to a local variable of "type" i
have.
Through reflection i can see the base type, but I don't know how to
perform a typecast from a string or type object

Type type = Type.GetType("BaseObject");
object Boxed = Activator.CreateInstance(type);
invoke....

local var of type BaseObject = Boxed; // need cast

hope this made more sense

Aug 2 '06 #3
I still don't get it...

If you were somewhere able to declare the local variable x of type
MyClass, you MUST have used the type in code (thus, NOT as a string).

MyClass x;

So you must also be able to write a cast statement within this method.
x = (MyClass) obj;
What did I get wrong? You have pairs of classes not sharing an
interface, but the same semantic information. I guess something similar
to WinClassXY and WebClassXY.

Now you have written a method to transform these objects into each
other, like
public static object TransformMyObj(object o1, Type t1, Type t2)
which returns the transformed object of type t2. What's next?

Maybe you can explain your problem using a complete method where it
applies. Then you write which types you do know, and which are only
present as strings by reflecton.

Cheers,
Stefan
ak*****@gmail.com schrieb:
I have many classes almost identical in pairs, but in different
namespaces. ( Interface dont work for this.)

It is a kind of copy constructor, Im traveling down the class and
coping the primitive types or change the class for there parallel
complex types.

This gives me a new object (class2) of the new type but with the other
classes data (class1).
but the new object is boxed (object) And i need it as its basetype. but
it has to be casted according to the object i put into the box from the
beginning.

Basicly im creating a class of a specific type, and filling it via
invokation of it.
Now i would like to assign this object to a local variable of "type" i
have.
Through reflection i can see the base type, but I don't know how to
perform a typecast from a string or type object

Type type = Type.GetType("BaseObject");
object Boxed = Activator.CreateInstance(type);
invoke....

local var of type BaseObject = Boxed; // need cast

hope this made more sense
Aug 2 '06 #4
You are right in your assumptions.

I do have an initial class that i want to transform to the parallel
type.

It is for a generic server with multibel accessinterfaces. One of
these beeing a webservice interface.

The input and out put is carried in reply and response classes, some of
these containing several nestede ,and unknown levels of layers.

Thus I have a request object for the server, and a simular request
object containing serialization, and other webservice related
information.

My first attempt was to create a copy constrctor for each class, and
use that for copying the data. However the data structure is still
changing from the customer, and this makes it easy to introduce errors
if a change is missed.

For this reason it would be much easier to have a static methode where
i pass in the source object. Through reflection I create a new object
of the destination object and fill it up. the methode is calles
recursively for each subclass. the returned object for each call then
must be cast to its base. I only know what type of class im copying
from the source class name, so I would like to make an auto typecast.

I don't know if it did give you any idear

I got a response from a guy saying that is properly is close to
imossible to do it, but if you have a idea how it can be done I would
be ever greatfull to hear about it.

Anyway thanks for taking the time.

Aug 2 '06 #5

ak*****@gmail.com wrote:
Hello there.

This should be simple, but im having trouble anyway of getting it to
work.
I have a boxed object that i want to cast to its native type, but I
would like to use reflection to do the typecasting.
This is the regular way.
object obj = new Foo();
... some dynamic invokation
Foo real = (Foo)obj;
real.DoSomething()
I would like it to do like the following
object obj = new Foo();
...
Foo real = (cast to typeof obj.getType() ) obj;
real.DoSomething()
Is it possible to do this dynamicly?
What's wrong with the following (without the cast):

MethodInfo doSomethingInfo = obj.GetType().GetMethod("DoSomething");
doSomethingInfo.Invoke(obj, new object[]);

or words to that effect?

Aug 3 '06 #6

ak*****@gmail.com wrote:
You are right in your assumptions.

I do have an initial class that i want to transform to the parallel
type.

It is for a generic server with multibel accessinterfaces. One of
these beeing a webservice interface.

The input and out put is carried in reply and response classes, some of
these containing several nestede ,and unknown levels of layers.

Thus I have a request object for the server, and a simular request
object containing serialization, and other webservice related
information.

My first attempt was to create a copy constrctor for each class, and
use that for copying the data. However the data structure is still
changing from the customer, and this makes it easy to introduce errors
if a change is missed.

For this reason it would be much easier to have a static methode where
i pass in the source object. Through reflection I create a new object
of the destination object and fill it up. the methode is calles
recursively for each subclass. the returned object for each call then
must be cast to its base. I only know what type of class im copying
from the source class name, so I would like to make an auto typecast.

I don't know if it did give you any idear

I got a response from a guy saying that is properly is close to
imossible to do it, but if you have a idea how it can be done I would
be ever greatfull to hear about it.

Anyway thanks for taking the time.
The basic problem here is one I've read about often here: for every web
service you call, the Designer creates separate classes, when in fact
some of the web services may deal with common types.

I haven't dealt with this myself (I will be soon), but if I were you I
would back up a step and do some digging here and elsewhere on the Web
to answer the question, "How can I have multiple web services share the
same types?" It's a common problem with, I imagine, a common solution.

Aug 3 '06 #7
Thanks Bruce

There is nothing wrong with the lines you wrote, but the problem is not
to invoke the methode, but to cast the returned object to a specific
type.
Myobject my

object = my;
invoke .... (not the problem)

Myobject o = (cast to boxing type via string name or type object) obj;

Aug 3 '06 #8
As I said, post some CODE! Where exactly is it a problem not to cast the
object to it's real type?
Either you don't know the type at all, so you will neither be able to
declare a local variable of the type nor to cast the object to assign it
to this (non-existing) variable. Remember: After creation the object
already has the type MyClassX, whatever variable you use to reference
it. The reflection system will always recognize the type of the object,
even though it is stored in a variable of type object.

You can use the FieldInfo.SetField(obj, obj)-method to assign values to
your newly created object's fields. There's no need for a real casting
in code.
When you eventually return from your copy-function, you should be able
to do a "normal" casting in your code.

As far as I understand your problem, you do not know the type (at design
time, in code!) of either object you have in your Copy-method.

I repeat: post some code (if possible, compilable code)! It's like
pictures: sometimes saying more than a thousand words...

Cheers,
Stefan

ak*****@gmail.com schrieb:
You are right in your assumptions.

I do have an initial class that i want to transform to the parallel
type.

It is for a generic server with multibel accessinterfaces. One of
these beeing a webservice interface.

The input and out put is carried in reply and response classes, some of
these containing several nestede ,and unknown levels of layers.

Thus I have a request object for the server, and a simular request
object containing serialization, and other webservice related
information.

My first attempt was to create a copy constrctor for each class, and
use that for copying the data. However the data structure is still
changing from the customer, and this makes it easy to introduce errors
if a change is missed.

For this reason it would be much easier to have a static methode where
i pass in the source object. Through reflection I create a new object
of the destination object and fill it up. the methode is calles
recursively for each subclass. the returned object for each call then
must be cast to its base. I only know what type of class im copying
from the source class name, so I would like to make an auto typecast.

I don't know if it did give you any idear

I got a response from a guy saying that is properly is close to
imossible to do it, but if you have a idea how it can be done I would
be ever greatfull to hear about it.

Anyway thanks for taking the time.
Aug 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Parag Joshi | last post by:
hi, I am facing a wierd problem with dyanamic class loading. I have a namespace called ETS.DAO which contains my Data Access Object classes. All these classes implement a common interface which...
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
6
by: Kenneth Baltrinic | last post by:
I have the following routine that is always throwing the error at the end. It never returns from within the foreach loop because the if expression is always evaluating to false apparently. This is...
3
by: Ray Mitchell | last post by:
Hello, I have an array list whose elements are the following types: float int float int ....etc.
3
by: Laura T. | last post by:
The following code is driving me so crazy, that I'm thinking it's a "feature" or a.. ....dare I say it... a BUG. I'm using VS 2003. I've created a new value type (struct) called MyInt. MyInt has...
1
by: cantonarv | last post by:
Just trying to do a basic crystal report and viewing it with dynamic sql server database but dont no what i'm doing wrong? I am using integrated security. Dim myRpt As crpt1 = New crpt1 ...
8
by: George Meng | last post by:
I got a tough question: The backgroud for this question is: I want to design an application works like a engine. After release, we can still customize a form by adding a button, and source code...
11
by: Manikandan | last post by:
Hi, I am using the following snippet to compare an object with integer in my script. if ( $forecast < 4 ) { I got the "segmentation fault" error message when i executed this script in CLI....
3
by: Trev | last post by:
Hi, I have a series of functions which do the following: ValidateData( args ); //args if of type ArrayList // Work out if the data is valid or not, and work out the type of args - int, string,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.