470,609 Members | 1,993 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,609 developers. It's quick & easy.

Dynamic Casting

Hi,

I have a need for "dynamic type casting": in other words, in a "MyConvert"
method I get passed an Object "value" and a Type "type" and the method
should attempt to convert value into type.

Of course it first tries to obtain the appropriate TypeConverter. However,
for some types there are no applicable type converters.
Consider a custom class "Dummy" from a linked assembly (in my scenario,
neither the Dummy type nor its assembly are "known", meaning any Type from
any assembly could be passed). There is no TypeConverter associated with
Dummy, however, it defines implicit cast operators to convert between Dummy
and String. I would like to know whether there is a standard way of
performing such a "dynamic" or "runtime" cast. I tried Convert.ChangeType()
but that method only works with IConvertible types---and completely ignores
any cast operators defined on any of the two types (that of value, and the
target Type).

Any ideas?
Many thanks,
Phil
Nov 17 '05 #1
6 6190
I gather there is no built-in way to do this?...

"Philipp Schumann" <ph**@mokka.org> schrieb im Newsbeitrag
news:uP**************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a need for "dynamic type casting": in other words, in a "MyConvert"
method I get passed an Object "value" and a Type "type" and the method
should attempt to convert value into type.

Of course it first tries to obtain the appropriate TypeConverter. However,
for some types there are no applicable type converters.
Consider a custom class "Dummy" from a linked assembly (in my scenario,
neither the Dummy type nor its assembly are "known", meaning any Type from
any assembly could be passed). There is no TypeConverter associated with
Dummy, however, it defines implicit cast operators to convert between
Dummy and String. I would like to know whether there is a standard way of
performing such a "dynamic" or "runtime" cast. I tried
Convert.ChangeType() but that method only works with IConvertible
types---and completely ignores any cast operators defined on any of the
two types (that of value, and the target Type).

Any ideas?
Many thanks,
Phil

Nov 17 '05 #2
Dan
How would the code following the cast know how to treat a reference
that it does not have type information for at compile time? I'm pretty
sure that there is no way to dynamically cast the way you want it to.
I have to do similar things to what you describe in my projects. I
have a manager that will call objects that were created well after the
manager was compiled. I have found that I can work around this issue
using interfaces/base classes and solve the problems in a more type
safe way. To convert your dummy class to a string the easiest way to
do what you want is override .ToString(). In that case you can use the
base class to provide the interface to provide the conversion.

If all of your dummy classes implement the same conversions, try
creating and implementing 1 or more interfaces like IToDecimal,
IToDateTime etc. It sucks that you cannot define operators in an
interface, but you can do the same work with methods and properties
(though not as pretty)

HTH,
Dan

Nov 17 '05 #3
You don't need a dynamic cast. You need to fix your design....
[class Dummy] defines implicit cast operators to convert between Dummy
and String.

implicit cast operators? eek.. Replacement them with explicit
conversion functions, say ToString & FromString. Define an interface, have
Dummy (and your other classes) implement that interface.

interface IToFromString
{
string ToString();
void FromString(string str);
}

public class Dummy : IToFromString
{
public override string ToString() {.... }
public void FromString(string str) {.....}
}
ThatFunctionInADifferentAssembly(IToFromString obj)
{
Console.WriteLine(obj.ToString());
}

--
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Philipp Schumann" <ph**@mokka.org> wrote in message
news:uP**************@TK2MSFTNGP14.phx.gbl... Hi,

I have a need for "dynamic type casting": in other words, in a "MyConvert"
method I get passed an Object "value" and a Type "type" and the method
should attempt to convert value into type.

Of course it first tries to obtain the appropriate TypeConverter. However,
for some types there are no applicable type converters.
Consider a custom class "Dummy" from a linked assembly (in my scenario,
neither the Dummy type nor its assembly are "known", meaning any Type from
any assembly could be passed). There is no TypeConverter associated with
Dummy, however, it defines implicit cast operators to convert between Dummy and String. I would like to know whether there is a standard way of
performing such a "dynamic" or "runtime" cast. I tried Convert.ChangeType() but that method only works with IConvertible types---and completely ignores any cast operators defined on any of the two types (that of value, and the
target Type).

Any ideas?
Many thanks,
Phil

Nov 17 '05 #4
Who says it is my design? My design just wants to work with ANY scenario, so
whether or not I _personally_ prefer the convenience of cast operators
(which can mean a lot _less_ overload definitions for many functions)
doesn't matter here. That said, the Dummy class was a moniker for all the
unknown types the API will need to work with, rather than a smallish design
of my own which I can change as I (or you) wish...

The whole issue actually makes me wonder whether type conversions are C#
syntactic sugar rather than supported at the framework level. Almost
certainly seems to be the case, will have to check that.

"James Curran" <ja*********@mvps.org> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP12.phx.gbl...
You don't need a dynamic cast. You need to fix your design....
[class Dummy] defines implicit cast operators to convert between Dummy

and String.

implicit cast operators? eek.. Replacement them with explicit
conversion functions, say ToString & FromString. Define an interface,
have
Dummy (and your other classes) implement that interface.

interface IToFromString
{
string ToString();
void FromString(string str);
}

public class Dummy : IToFromString
{
public override string ToString() {.... }
public void FromString(string str) {.....}
}
ThatFunctionInADifferentAssembly(IToFromString obj)
{
Console.WriteLine(obj.ToString());
}

--
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Philipp Schumann" <ph**@mokka.org> wrote in message
news:uP**************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a need for "dynamic type casting": in other words, in a
"MyConvert"
method I get passed an Object "value" and a Type "type" and the method
should attempt to convert value into type.

Of course it first tries to obtain the appropriate TypeConverter.
However,
for some types there are no applicable type converters.
Consider a custom class "Dummy" from a linked assembly (in my scenario,
neither the Dummy type nor its assembly are "known", meaning any Type
from
any assembly could be passed). There is no TypeConverter associated with
Dummy, however, it defines implicit cast operators to convert between

Dummy
and String. I would like to know whether there is a standard way of
performing such a "dynamic" or "runtime" cast. I tried

Convert.ChangeType()
but that method only works with IConvertible types---and completely

ignores
any cast operators defined on any of the two types (that of value, and
the
target Type).

Any ideas?
Many thanks,
Phil


Nov 17 '05 #5
Who says it is my design? My design just wants to work with ANY scenario, so
whether or not I _personally_ prefer the convenience of cast operators
(which can mean a lot _less_ overload definitions for many functions)
doesn't matter here. That said, the Dummy class was a moniker for all the
unknown types the API will need to work with, rather than a smallish design
of my own which I can change as I (or you) wish...

The whole issue actually makes me wonder whether type conversions are C#
syntactic sugar rather than supported at the framework level. Almost
certainly seems to be the case, will have to check that.

"James Curran" <ja*********@mvps.org> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP12.phx.gbl...
You don't need a dynamic cast. You need to fix your design....
[class Dummy] defines implicit cast operators to convert between Dummy

and String.

implicit cast operators? eek.. Replacement them with explicit
conversion functions, say ToString & FromString. Define an interface,
have
Dummy (and your other classes) implement that interface.

interface IToFromString
{
string ToString();
void FromString(string str);
}

public class Dummy : IToFromString
{
public override string ToString() {.... }
public void FromString(string str) {.....}
}
ThatFunctionInADifferentAssembly(IToFromString obj)
{
Console.WriteLine(obj.ToString());
}

--
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Philipp Schumann" <ph**@mokka.org> wrote in message
news:uP**************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a need for "dynamic type casting": in other words, in a
"MyConvert"
method I get passed an Object "value" and a Type "type" and the method
should attempt to convert value into type.

Of course it first tries to obtain the appropriate TypeConverter.
However,
for some types there are no applicable type converters.
Consider a custom class "Dummy" from a linked assembly (in my scenario,
neither the Dummy type nor its assembly are "known", meaning any Type
from
any assembly could be passed). There is no TypeConverter associated with
Dummy, however, it defines implicit cast operators to convert between

Dummy
and String. I would like to know whether there is a standard way of
performing such a "dynamic" or "runtime" cast. I tried

Convert.ChangeType()
but that method only works with IConvertible types---and completely

ignores
any cast operators defined on any of the two types (that of value, and
the
target Type).

Any ideas?
Many thanks,
Phil


Nov 17 '05 #6
Philipp Schumann <ph**@mokka.org> wrote:
Who says it is my design? My design just wants to work with ANY scenario, so
whether or not I _personally_ prefer the convenience of cast operators
(which can mean a lot _less_ overload definitions for many functions)
doesn't matter here. That said, the Dummy class was a moniker for all the
unknown types the API will need to work with, rather than a smallish design
of my own which I can change as I (or you) wish...

The whole issue actually makes me wonder whether type conversions are C#
syntactic sugar rather than supported at the framework level. Almost
certainly seems to be the case, will have to check that.


You can use reflection to find members with special names - they may
well help you. The operator is compiled into a method, just a special
one...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Jason Tesser | last post: by
5 posts views Thread by Radde | last post: by
2 posts views Thread by Zac | last post: by
3 posts views Thread by Jeff Poste | last post: by
4 posts views Thread by Val | last post: by
2 posts views Thread by Martin Hart - Memory Soft, S.L. | last post: by
4 posts views Thread by Zark3 | last post: by
2 posts views Thread by Mike Stevenson | last post: by
28 posts views Thread by hlubenow | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.