Cast from name syntax

  • Thread starter Thread starter Nick Weekes
  • Start date Start date
N

Nick Weekes

Hi all,

I am trying to cast a Double (its actually the NextDouble method of the
Random class, but could be any type of number) to a Type via its name.

Im trying the following syntax:

ThisNumber = (Type.GetType("MyType.Decimal"))(ThisRandom.NextDouble());

Im getting an error stating that 'Method name expected', so either im
not casting correctly or using incorrect syntax?

Thanks,

Nick
 
Nick,

when you use type casting using the syntax: "(<type>) obj" or "obj as
<type>", the <type> name has to be know during compilation. In other word
you can use (int)var, but cannot use (typeof(int))var or something like
that.

BTW in your code sample you do know the type you want to covert to it is
MyType.Decimal. Why don't you use

ThisNumber = (MyType.Decimal)(ThisRandom.NextDouble()); this will compile as
long as there is conversion between double and MyType.Decimal.

There is no general conversion from any type to any type. Usually there are
two types of dynamic conversion. One is when a type implements IConvertible
and the other is when a type has TypeConverter attached to it via an
attribute.

Since you want to convert from double to your own type IConvertible won't
work for you. Thus what you might wanna do is to create a TypeConverter for
your own type.

However, look again your design. I've found that in most of the cases when
programmers think that they don't know the type they want to convert to at
compile time, this is simply not the case. Look again the code you've
posted. I know that this is just a sample and the real world scenario could
be more complex.



Stoitcho Goutsev (100) [C# MVP]
 
Stoitcho,

Thanks for the explanation. Why is it not possible to cast an unknown,
for example (typeof(MyType))var?

Cheers,

Nick
 
First the difference between casting and converting.

When you are casting, you are saying that you know object X is really of
type Y. You are in effect telling the complier that it is safe to assign
object X to variable Z. There is no change to the underlying object.

Y Z = (Y)X;

When you are converting, you are creating a new object that has the same
value as the old one, but is in a different format. For example, when making
an integer into a double.
Thanks for the explanation. Why is it not possible to cast an unknown,
for example (typeof(MyType))var?

You aren't really casting in the design sense because you cannot make that
promise. Besides, when would you not know what type you wanted to cast to?

(You are still casting in the runtime sense. That is, the runtime is
verifying the cast is safe and the types are equivalent.)
 
This is just a language syntax, but I guess the reason is that the IL
instruction that this expression emits expects a type metadata token. It
cannot be generated at compile type if you use a variable that doesn't have
value at compile time. Keep in mind that the IL instruction for conversion
*castclass* converts only to/from classes and itnerfaces that are in
base/derived class relationship or the interface is implemented by the type.
All other convrsion techniques like IConvertible, TypeConverter or type
casting operators overloading (C#) is not taken into account. All such
conversions has to be done explicitly by the progeammer or the compiler.
They are not part of the .NET Runtime so to speak.

Dynamic conversion cannot be provided out of the box. The one who writes the
class only know how to convert it to/from another type. For example how
could the runtyme know how to cnonvert from string to Foo. Only me (the
programmer who wrote this foo class) knows how to do that and if this
conversion makes any sense after all. The framework provides an
infrastructure for dynamic conversion
IConvertible and Convert class, or TypeConverter, but the implementation is
up to the developer.

The sample that I gave
(typeof(MyType))var?

doesn't make much sense because this line can be re-written simply to
(MyType)var and it will compile.

but this (what actually I wanted to demonstrate) is not possible

Type t = var1.GetType();
var1 = (t)var2; // at compile tyme t doesn't have value.
 
Back
Top