Casting an Object type to DataRow & IDatareader
+1
A:
You cant just cast an object to either of those you mentioned. They have to be either of those or derive from those types.
There is no alchemy in programming.
leppie
2010-08-12 05:20:45
object reader1 reader1 = Convert.ChangeType(reader1, Type.GetType(reader1.ToString())); //(object)Convert.ChangeType(MyDataSet, typeof(object)); //(System.Data.DataRow)Convert.ChangeType(reader1, Type.GetType(reader1.ToString())); //(Type.GetType(reader1))Convert.ChangeType(reader1, Type.GetType(reader1.ToString()));
Charu
2010-08-12 05:30:38
i thing, we can do like the above example
Charu
2010-08-12 05:31:01
@Charu: No, that is very wishful thinking.
leppie
2010-08-12 06:35:30
A:
Casting can only happen between compatible types. To transform two incompatible types you need to perform conversion.
Ex: You cannot cast a string into int and vice versa since they are incompatible types so there is a way of converting both into one another.
int x = 5;
string str = string.Empty;
str = x.ToString(); //Convert int x into string str.
x = Convert.ToInt32(str); //Convert string str into int x.
Instead of writing the whole story explaining difference between casting and conversion here, I'd rather recommend you to read this interesting conversation on SO. Do read Eric Lippert's answer. Eric Lippert works with Microsoft in C# compiler development team.
this. __curious_geek
2010-08-12 05:24:43