Re: Dynamic casting in c#
- From: andrewbb@xxxxxxxxx
- Date: 24 Oct 2005 16:47:04 -0700
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!
.
- Follow-Ups:
- Re: Dynamic casting in c#
- From: Jon Skeet [C# MVP]
- Re: Dynamic casting in c#
- From: Vipul Patel
- Re: Dynamic casting in c#
- References:
- Dynamic casting in c#
- From: andrewbb
- Re: Dynamic casting in c#
- From: Jon Skeet [C# MVP]
- Dynamic casting in c#
- Prev by Date: Re: Support for optional parameters
- Next by Date: Re: Using structure as hashtable key
- Previous by thread: Re: Dynamic casting in c#
- Next by thread: Re: Dynamic casting in c#
- Index(es):
Relevant Pages
|