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

BinaryWriter - Dynamic cast
Vitalijus
um...no. The problem is that BinaryWriter.Write(string) and BinaryWriter.Write(int) are completely different functions. Pretend as if they were called BinaryWriter.WriteString(string) and BinaryWriter.WriteInt(int). The only way to call the right method is to use the big switch statement.
You can cheat and code it like : objBinaryWriter.Write(obj.ToString());