You can't do this. You would have to check the type and have a big
switch statement.
Or, if you want, and don't mind the performance hit, you could use
reflection. Get the type for BinaryWriter and find the method which takes
one parameter of the type of obj.GetType, then call that method, passing in
your object.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
(E-Mail Removed)
"jfbaro" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I am new to C# and I don't know how to cast dynamically an object to a
> specific type . I have the object and its type (as a Type object).
>
> For example, I read some data from a query (which has been created
> dynamically) and get a list of objects and theirs types (string, Int64,
> DateTime...). Now I need to put them into a BinaryWrite. Is there any
> way of dynamically define which Write method to call?? I don't want to
> have a BIG switch to test each possible Type.
>
> I would like to have something similar to that:
>
> Object obj = new Object(); \\ it comes from a datareader... let's
> assume it is a string
> objBinaryWriter.Write(obj as obj.GetType());
>
> Thanks in advance
>