NB - if you are reading this on the DOTNET list, please note that this list
shuts down fairly soon.  I have cross-posted this to the DOTNET-CX list.
Please reply on that list.  Thanks.


Can I ask what you actually expect to happen on this line here:

>           //cast the object to the type defined by the string.
>      MyVar=(Type.GetType(whatsthetype)) foo;

I suspect that you may be falling into one of C#'s more subtle traps for the
unwary:  'casting' can mean 2 completely different things according to the
context.

Some casts are conversions.  Conversion is, roughly speaking, creating a new
instance which is of a different type from the item being converted, but
which will in some sense have the same value. E.g.

  double d = 42.0;
  int i = (int) d;

This 'cast' converts the double d to the integer i.  This is a numeric
conversion.  It creates a new integer (and in this case stores that integer
value in the variable i) which has as close a value to the double that is
being converted as is possible.  (In this particular case the number can be
represented precisely by an int because 42 is an integer, but in general the
conversion may have to be approximate.)

But in other contexts, casts are not conversions.  Consider this:

  object o = "Hello, world";
  Console.WriteLine (o.GetType());
  string s = (string) o;    // CAST
  Console.WriteLine (s.GetType());

Here nothing is converted at all - there is just the one object, and objects
never change their types, as will be evident from the output to the Console.
Once a string is created it remains a string for its whole life.  So the
line labelled CAST in this example does something different from the
previous cast despite having apparently identical syntax.  In this case no
new instance is created at all - we just have the one object.  All that this
cast is doing is getting a new reference of a different type.  Our first
reference is of type object, our second is of type string.  But they both
refer to the same object.  (Remember that a reference doesn't have to be the
*same* type as the object it refers to, it merely has to be of a type with
which the object is compatible.)  This cast operation here causes the C#
compiler to generate code which will do a runtime check - it will check to
see if the object referred to by o is compatible with the type string.  If
it is, the cast succeeds (we now have a reference of type string to the
object), otherwise it throws an exception.

So how do you know which will happen.  Well this non-converting cast is the
norm - most of the time when you do a cast you will be getting a new kind of
reference to an existing object.  There are three exceptions: (1) intrinsic
numeric conversions - the set of known conversions between the intrinsic
numeric types; these are baked into the C# compiler, (2) user conversions -
it is possible to add conversion operators to your own types.  If you are
not using such types these will never be an issue.  (3) boxing - casting a
value type to object causes a boxed copy of the value to be created; these
can be particularly insidious because casts to object are allowed to be
implicit.  (E.g. object o = 42; will cause an implicit cast from int to
object, and thus an implicit box.)  Boxing is only an issue with value
types; it is also not a conversion as such - the box's type is the same as
the value's type, the only potential problem being that you get an implicit
copy.

(Pedants might wish to regard the default reference casting as being just
another kind of conversion - converting a reference to another type of
reference.  This is a reasonable point of view, but most developers tend to
think of the reference as a shorthand for the referent most of the time,
something which is somewhat encouraged by C#'s syntax, so this is not a
particularly natural way of thinking about it.  Sadly, there is also a
subset of these developers don't understand the difference between the
reference and the thing being referred to, but that's another issue.)


In your example it's not entirely clear what
SomeFunctionThatReturnsAnObjectType returns.  Does it return an object whose
type is System.Type?  Or does it return some object that implements IBar?
If the latter, then as has already been pointed out, you don't need to do
any conversion.  Just cast to IBar (which will do a non-converting cast.)


--
Ian Griffiths
DevelopMentor

----- Original Message -----
From: "Steve Holak" <[EMAIL PROTECTED]>


> I'm banging my head on something simple and don't have my references here:
>
> I have an object type returned by a function that has just de-serialized
> that object from a database (Soap deserializer) .  The object implements a
> known interface (IBar).
>
> I know the type of the original object , but my knowledge of the type is
> represented as a string. I want to cast the object to the Type defined by
> the string, something like:
>
>
> void CastTheDamnThing(string whatsthetype)
> {
>      IBar MyVar;
>
>      object  foo=SomeFunctionThatReturnsAnObjectType();
>
>           //cast the object to the type defined by the string.
>      MyVar=(Type.GetType(whatsthetype)) foo;
>
> //do something else . . .
>
> }
>
> It obviously isn't right;
> What am I missing?  I feel dumb for asking, but it's the end of the day
and
> I'm not leaving until I figure out how to do this.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to