How to cast in c# (Just kidding). But I do have a non-trival casting question.

T

the.duckman

G'Day.

Anybodey got an idea on this problem.

Say I have a function object doCast(object obj, Type t);
It's job is to cast the obect (obj) to a new type (t) and return it.

Sounds so simple, but I cant seem to find a way to do it without some
prety extravigant hacks.

Can anybodey think of a simple solution that I am overlooking. (um for
..net 1.0)

-dm
 
T

the.duckman

Opps there is a .net has a function that does this.....
Convert.ChangeType(...).

My bad.

-dm
 
J

Jon Skeet [C# MVP]

Anybodey got an idea on this problem.

Say I have a function object doCast(object obj, Type t);
It's job is to cast the obect (obj) to a new type (t) and return it.

Sounds so simple, but I cant seem to find a way to do it without some
prety extravigant hacks.

Can anybodey think of a simple solution that I am overlooking. (um for
.net 1.0)

Well, normal casting (leaving aside actual conversions) wouldn't do you
any good, because the method signature would still have to be "object".
Are you looking to perform the various implicit and explicit
conversions?
 
T

Truong Hong Thi

Convert.ChangeType performs convertion, not simple cast, and requires
the object to implement IConvertible interface. If you simply want to
cast, the doCast function would make no sense since it returns object
and you have to cast the return value again.
Sounds so simple, but I cant seem to find a way to do it without some
prety extravigant hacks
I am curious what are those hacks?
 
?

=?iso-8859-1?Q?Lasse=20V=e5gs=e6ther=20Karlsen?=

G'Day.
Anybodey got an idea on this problem.

Say I have a function object doCast(object obj, Type t); It's job is
to cast the obect (obj) to a new type (t) and return it.

Sounds so simple, but I cant seem to find a way to do it without some
prety extravigant hacks.

Can anybodey think of a simple solution that I am overlooking. (um for
.net 1.0)

-dm

What would be the difference from just doing:

SomeObject x;
SomeOtherObject y = (SomeOtherObject)x;

?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
Say I have a function object doCast(object obj, Type t);
It's job is to cast the obect (obj) to a new type (t) and return it.

Sounds so simple, but I cant seem to find a way to do it without some
prety extravigant hacks.

Can anybodey think of a simple solution that I am overlooking. (um for
.net 1.0)

You will have to return an object reference, so I find the method itself
pretty useless as it's right now. What are you trying to do?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

What would be the difference from just doing:

SomeObject x;
SomeOtherObject y = (SomeOtherObject)x;

The OP do not have SomeOtherObject nor he knows what it's , all he has is a
Type instance representing it.
 
T

the.duckman

Hello again, I thought is had solved the problem but as pointed out by
Truong it only works for classes implementing IConvetable.

Basicly I need to convert an object to a new type where the "new type"
is determined at runtime.

I think this is actuly a challange now...

basicly complete this stub:


object doCast(object obj, Type t)
{
object obj;

///magic code goes here to conver obj to type t

return obj;
}



The person with the most elegant solution will win.... "New Zealand".
Dont miss your chance to own your own pacific island nation!

-dm
 
T

the.duckman

opps (bad stub, lets correct),

object doCast(object obj, Type t)
{
object newObj;

///magic code goes here to conver obj to type t

return newObj;
}


Idealy this would work....

newObj = obj as t;

-dm
 
J

Jon Skeet [C# MVP]

Hello again, I thought is had solved the problem but as pointed out by
Truong it only works for classes implementing IConvetable.

Basicly I need to convert an object to a new type where the "new type"
is determined at runtime.

Well, how do you expect that conversion to happen? What has the
knowledge to convert one type to another in your code?

For instance, suppose I have two classes:

FirstClass
{
int x;
}

SecondClass
{
string y;
}

How would you expect it to convert an instance of FirstClass into an
instance of SecondClass?
 
T

the.duckman

RE: How would you expect it to convert an instance of FirstClass into
an
instance of SecondClass?

In the usual way.....

If SecondClass or FirstClass have an appropriate cast operator than
that operator is used.

Otherwise invalidcastexception would be thrown.


-dm
 
T

the.duckman

This of couse makes one think of using getmethodinfo to retrive the
valid cast operattor and then invokeing it.... I just don't like messy
solutions to simple problems.

I could also emit my own code with the corect cast in it, again so very
messy...

There must be an easier way. Its such a simple problem.

-dm
 
B

Bill Butler

RE: How would you expect it to convert an instance of FirstClass into
an
instance of SecondClass?

In the usual way.....

If SecondClass or FirstClass have an appropriate cast operator than
that operator is used.

Otherwise invalidcastexception would be thrown.

Hi,

You keep using the terms CAST and CONVERT interchangeably.
They do not mean the same thing.

You convert an object into a different object based on some conversion rules.
There are 2 different objects involved
The original object
The converted object

When you cast an object to a different type there is only 1 object involved
Animal animal = new Dog();
Dog dog = (Dog)animal;

There is only one object in the above snippet, and both animal and dog reference it.

So,.....Are you attempting to Cast or Convert in your question?

Bill
 
T

the.duckman

I mean to say Cast as in the calling the cast operator (if it exists).

object doCast(object obj, Type t)
{
object newObj;

///magic code goes here to CAST obj to type t and assign it to
newObj

return newObj;
}

class dog
{
string name;
public static explicit operator carnivourusAnimal(dog d)
{
return new carnviourusAnimal(d.name, "beef roo mutton", "woof bark
howl");
}
}

//example useage
object dog = new dog();
Type streamAs = typeof(carnivourusAnimal);

if (protocol.ExplicitTypeRequired)
streamAs = protocol.TypeRequired;

object StreamObject = doCast(dingo, streamAs);
sendItem(StreamObject);
 
B

Bill Butler

I mean to say Cast as in the calling the cast operator (if it exists).

You keep saying that.
But your example is that of an explicit conversion....not a cast.

To late to think straight right now.
I'll look at your example again tomorrow
Bill
 
T

the.duckman

Thanks,

I guess we dont' exactly use the same lingo...

Lets define a term "transformation" (what I want)
The "transformation" is the act of calling:
implicit or explicit numeric conversions
implicit or explicit User-defined conversions.

This does not cover base class generalistaion (non-user-defined cast to
a parent type or to an implemented interface). The reason being that
the object is already of that type and hence our function would be of
no benifit. Notice that in my example dog did not derive from
carnivourusAnimal. If dog had derived from carnivourusAnimal then there
would be no poing in doing the cast sice the dog would already be a
carnivourusAnimal.

when the doCast function is called a serach is conducted for a suitable
"transformation" and that transformation is applied.

This is probably most usefull in situations where a comunications class
species a type which it expects to recive information in.

Basicly what I want is a way for objects "product" to be shaped into a
type that may be required by another process "consumer". The process
does not need to be altered to accomidate new classes that apear. The
process needs only to provide information on what type it requires.

The "item" class then only needs a suitable user-defined conversion and
its intergration into the application is done.

This is only usefull if new "item" classss are often indroduced into
the aplication and the what the "consumer" wants is determined at
runtime, and what the consumer expects can not be generalised to a base
class or common interface.

-dm
 
B

Bill Butler

Thanks,

I guess we dont' exactly use the same lingo...

Lets define a term "transformation" (what I want)
The "transformation" is the act of calling:
implicit or explicit numeric conversions
implicit or explicit User-defined conversions.

Fair enough, Transformation works fine
This does not cover base class generalistaion (non-user-defined cast to
a parent type or to an implemented interface). The reason being that
the object is already of that type and hence our function would be of
no benifit. Notice that in my example dog did not derive from
carnivourusAnimal. If dog had derived from carnivourusAnimal then there
would be no poing in doing the cast sice the dog would already be a
carnivourusAnimal.

I believe that this was the source of some confusion in some of the responses.
when the doCast function is called a serach is conducted for a suitable
"transformation" and that transformation is applied.

This is probably most usefull in situations where a comunications class
species a type which it expects to recive information in.

Basicly what I want is a way for objects "product" to be shaped into a
type that may be required by another process "consumer". The process
does not need to be altered to accomidate new classes that apear. The
process needs only to provide information on what type it requires.

The "item" class then only needs a suitable user-defined conversion and
its intergration into the application is done.

OK, I am confused again.
Is an "item" class different from a "product" class, or have we changed names????
This is only usefull if new "item" classss are often indroduced into
the aplication and the what the "consumer" wants is determined at
runtime, and what the consumer expects can not be generalised to a base
class or common interface.

OK,
I think the problem most of us are having is that you are asking for a general object to object
transform method/class. However, in general, most objects SHOULD NOT be convertible into any old
object. Don't get me wrong, You COULD write a general object transformer, but most of the cases
would need to throw some "Untransformable" Exception. And in general, the results would not be
pretty.
Of cause looking at your product/item/consumer description, it doesn't look like you really need
a generic transformer at all. So the real question is, "What do you really need?"

So let me ask a few questions, to narrow down the scope a bit.


Are we talking about external vendors? Web services? In house services? Are they separate processes
or just separate threads?
How many consumers are we talking about? Do they do the same thing, or different things?
Will CONSUMER_A always want the same type, or will it vary from call to call?

Is each product a different class?
Is there a common ancestor?

Is there a 1- to -1 mapping of objects from your side to the "consumer" side?
ProductA -->Consumer1ProductA
ProductA -->Consumer2ProductA
ProductB -->Consumer1ProductB
ProductB -->Consumer2ProductB

Or is more like this
ProductA -->Consumer1Product
ProductA -->Consumer2Product
ProductB -->Consumer1Product
ProductB -->Consumer2Product

Or is it something more complex?

I guess the bottom line is that depending on the problem you are attempting to solve, the best
solution may look quite different. However, I am fairly certain that a generic "Transformer" is
probably not the ideal solution. I am not saying that you don't need a means of converting types, I
am simply saying that it would be better to narrow down the scope to something smaller that
"anything to anything else".

Bill
 

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