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

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 6366
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Jason Tesser | last post by:
I work for at a college where I am one of 2 full-time developers and we are looking to program a new software package fro the campus. This is a huge project as it will include everything from...
5
by: Radde | last post by:
HI, Are ther any pitfalls for dynamic cast in type safe downcasting..
2
by: Zac | last post by:
Alright anyone who has 2c throw it in... I am working through a custom xml serializer and have come upon a conundrum, given our class design. The interface implemented on the base class (base...
3
by: Jeff Poste | last post by:
Hi, I'm developing software that requires business rules that constantly need new versions for different quarters, years, etc. I created a business rule factory that stores these different...
4
by: Val | last post by:
Hi I am using a DynamicProxy (class that inherits from RealProxy). I don't know the type of my Transparant Proxy at compile time. Is there a possibility to have a dynamic casting object...
2
by: Martin Hart - Memory Soft, S.L. | last post by:
Hi all: I still very new to the .NET world and don't know if what I am asking is due to an over-imaginative imagination or the fact that I have read too many fiction books!! Let me show you a...
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
2
by: Mike Stevenson | last post by:
Hi. I'm in the process of re-learning all the C++ I forgot from college, and I'm starting to get into some virgin (or at least only a couple times) territory. I have some questions about casting...
28
by: hlubenow | last post by:
Hello, I really like Perl and Python for their flexible lists like @a (Perl) and a (Python), where you can easily store lots of strings or even a whole text-file. Now I'm not a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.