Is it possible to do dynamic type casting in C#?
Say I have an overloaded method that takes an int or double param, but the param comes from a typed dataset that can contain a DBNULL. Just calling MyDataSet.myTable.MyField will throw an InvalidCastException when the underlying value in the DS is null.
I don't want to check each iteration for IsMyFieldNull, how can I deal with this?
I thought that if I could do some dynamic casting I could solve the problem, by returning an Empty String, but I can't.
Something like this: (Which obviously won't compile):
public static Object HandleDBNull(Object value)
{
if (value.Equals(DBNull.Value))
{
return String.Empty;
}
else
{
return value;
}
}