Dynamic Casting

P

Philipp Schumann

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
 
D

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
 
J

James Curran

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
 
P

Philipp Schumann

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 said:
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 said:
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
 
J

Jon Skeet [C# MVP]

Philipp Schumann said:
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...
 

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

Similar Threads


Top