473,698 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.DoSomethin g()
I would like it to do like the following
object obj = new Foo();
....
Foo real = (cast to typeof obj.getType() ) obj;
real.DoSomethin g()
Is it possible to do this dynamicly?

Aug 2 '06 #1
8 4233
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.c om 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.DoSomethin g()
I would like it to do like the following
object obj = new Foo();
...
Foo real = (cast to typeof obj.getType() ) obj;
real.DoSomethin g()
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("B aseObject");
object Boxed = Activator.Creat eInstance(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.c om 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("B aseObject");
object Boxed = Activator.Creat eInstance(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 accessinterface s. 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.c om 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.DoSomethin g()
I would like it to do like the following
object obj = new Foo();
...
Foo real = (cast to typeof obj.getType() ) obj;
real.DoSomethin g()
Is it possible to do this dynamicly?
What's wrong with the following (without the cast):

MethodInfo doSomethingInfo = obj.GetType().G etMethod("DoSom ething");
doSomethingInfo .Invoke(obj, new object[]);

or words to that effect?

Aug 3 '06 #6

ak*****@gmail.c om 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 accessinterface s. 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.SetFi eld(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.c om 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 accessinterface s. 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
1400
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 is also in the same namespace. I have a namespace ETS.Controller which contain my comtroller classes. My Web client instantiates the controller who in turn fires appropriate methods on the DAOs. In the controller i am using dynamic class loading...
1
2222
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 pertaining to casting class A to class B, the implementation of the
6
1976
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 something of a problem. The comparison is between two objects (Handler.Data is defined as returning an object.), but these objects are always nothing more than boxed value types (either strings or ints or an int based enumeration). If for the...
3
3236
by: Ray Mitchell | last post by:
Hello, I have an array list whose elements are the following types: float int float int ....etc.
3
1635
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 all(?) the conversion routines necessary to convert from Int32 and string. (see the listing at the end) When I cast directly from a value of Int32 type, it works:
1
4909
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 'Where crpt1 is an existing crystal report 'set databse interaction with crystal Crystalviewer1.LogOnInfo.Item(0).ConnectionInfo.DatabaseName =
8
2013
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 for the button. (This is done by the form itself, not by using VS.Net) (button and source code should be a record in database, these information should be retrieve from database when the form shows up) What I want is:
11
1924
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. After debugging, i had changed my code to
3
2102
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, whatever? if((bool)args) // valid data { WriteDataToDB( args );
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9026
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.