Re: Dynamic casting in c#

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



But I will know more than the compiler at runtime: I know the type it
should be cast to. The reason I don't know at compile time is because
the result of the cast is set on an instance of an unknown object of
unknown type. eg. myFieldInfo.SetValue(myObject, val).

ADO.NET handles this under the hood for Guid and DateTime (maybe other
custom types as well) and creates an object of type Guid or DateTime
when it builds the dataset, assigning that value to the row/col.

I've found a workaround for enums (Enum.Parse(typeof(MyEnumType),
val)), but this still seems lacking in the language. I see no reason
to not be able to cast something dynamically at runtime.

The whole purpose is for a persistence layer that stores/loads any
object to/from any back-end data storage. (XML and SqlServer are
implemented at the moment). Set a [Persistable] attribute on any class
member and the persistence layer takes care of everything else. With
the Enum workaround I suppose it's solved... All major data types that
you might want to persist are now supported!

.



Relevant Pages

  • Re: Polymorphism
    ... If this is the reason, ... your post you say "I presume the reason is that the type to cast to is not ... not compile time. ...
    (comp.lang.java.programmer)
  • Re: Type cast to a target type from just its string representation
    ... "Sathyaish" wrote in message ... The only reason why you would perform the cast is to assign the result to a variable of that precise type: ... And if you don't know the type at compile time, you can only declare the variable as object, so you don't need any cast: ...
    (microsoft.public.dotnet.languages.csharp)
  • Polymorphism
    ... I presume the reason is that the type to cast to is not known at ... compile time. ... public void foo{ ...
    (comp.lang.java.programmer)
  • Re: Does anyone else watch shows just to pine over a hottie?
    ... for the sole reason of looking at one of the girls: ... For me, it's Premise then Crew then Cast. ... actors I care about in the roles is highly important to me. ...
    (rec.arts.tv)
  • Re: Option Strict issue
    ... The reason is that Rowreturns an object. ... Cast is as an ... option strict is On ... Éric Moreau, MCSD, Visual Developer - Visual Basic MVP ...
    (microsoft.public.dotnet.languages.vb)