Dynamic type casting at run time

  • Thread starter Thread starter Earl Teigrob
  • Start date Start date
E

Earl Teigrob

I am using reflection to iterate the fields of a class and need to cast
these (sqldata) types to there .net primitive counterparts. The code I am
currently using for this is certainly a brute force method but it does work.
Is there a more elegant way of doing this? (See code below)

Thanks

Earl

string p;
foreach (System.Reflection.FieldInfo myFieldInfo in
DataRecord.GetType().GetFields())
{
object x = myFieldInfo.GetValue(DataRecord);
if (!SqlTypeIsNull(x))
{
p=x.GetType().ToString();
switch (p)
{
case "System.Data.SqlTypes.SqlBoolean":
r[myFieldInfo.Name]=(Boolean)(System.Data.SqlTypes.SqlBoolean)x;
break;
case "System.Data.SqlTypes.SqlByte":
r[myFieldInfo.Name]=(Byte)(System.Data.SqlTypes.SqlByte)x;
break;
case "System.Data.SqlTypes.SqlDateTime":
r[myFieldInfo.Name]=(DateTime)(System.Data.SqlTypes.SqlDateTime)x;
break;
case "System.Data.SqlTypes.SqlDecimal":
r[myFieldInfo.Name]=(Decimal)(System.Data.SqlTypes.SqlDecimal)x;
break;
case "System.Data.SqlTypes.SqlDouble":
r[myFieldInfo.Name]=(Double)(System.Data.SqlTypes.SqlDouble)x;
break;
case "System.Data.SqlTypes.SqlInt16":
r[myFieldInfo.Name]=(Int16)(System.Data.SqlTypes.SqlInt16)x;
break;
case "System.Data.SqlTypes.SqlInt32":
r[myFieldInfo.Name]=(Int32)(System.Data.SqlTypes.SqlInt32)x;
break;
case "System.Data.SqlTypes.SqlInt64":
r[myFieldInfo.Name]=(Int64)(System.Data.SqlTypes.SqlInt64)x;
break;
case "System.Data.SqlTypes.SqlSingle":
r[myFieldInfo.Name]=(Single)(System.Data.SqlTypes.SqlSingle)x;
break;
case "System.Data.SqlTypes.SqlString":
r[myFieldInfo.Name]=(String)(System.Data.SqlTypes.SqlString)x;
break;
default:
r[myFieldInfo.Name]=(String)(System.Data.SqlTypes.SqlString)x;
break;
}
}
}
 
Why do you need to cast them only to put them back into an object?
p=x.GetType().ToString();

Best not to use a string here, you've just thrown any sort of compiler checking out the window.
case "System.Data.SqlTypes.SqlString":
r[myFieldInfo.Name]=(String)(System.Data.SqlTypes.SqlString)x;
break;
default:
r[myFieldInfo.Name]=(String)(System.Data.SqlTypes.SqlString)x;
break;

Why duplicate this?
 
Michael,

Thanks for your response...and well, thats why a make this post, to give me
a better way of doing this.. so can you give me an example of what you are
taking about?

Thanks

Earl

Michael Culley said:
Why do you need to cast them only to put them back into an object?
p=x.GetType().ToString();

Best not to use a string here, you've just thrown any sort of compiler checking out the window.
case "System.Data.SqlTypes.SqlString":
r[myFieldInfo.Name]=(String)(System.Data.SqlTypes.SqlString)x;
break;
default:
r[myFieldInfo.Name]=(String)(System.Data.SqlTypes.SqlString)x;
break;

Why duplicate this?
 
Earl Teigrob said:
Michael,

Thanks for your response...and well, thats why a make this post, to give me
a better way of doing this.. so can you give me an example of what you are
taking about?

I don't think what you are doing will have any affect, eg

object X = true;
object Y = (bool) X;

will be the same as just

object X = true;
object Y = X;

You don't need the type cast because it is going back into an object, although I don't understand your double type cast, it seems to
cast it to an enum and then a string?:

=(String)(System.Data.SqlTypes.SqlString)
checking out the window.

You would be better using:

Type myType = X.GetType();
if (myType == typeof(sqlTypes.Int)) DoSomething;
etc

that way if you mistype sqlTypes.Int (as I probably have) then the compiler will tell you.
 
Michael Culley said:
You don't need the type cast because it is going back into an object,
although I don't understand your double type cast, it seems to
cast it to an enum and then a string?:

=(String)(System.Data.SqlTypes.SqlString)

That's because the object itself is a SqlString, which can then be
explicitly converted to a String. It looks odd, but it *does* actually
make a difference.
You would be better using:

Type myType = X.GetType();
if (myType == typeof(sqlTypes.Int)) DoSomething;
etc

that way if you mistype sqlTypes.Int (as I probably have) then the
compiler will tell you.

True - but you end up with a nasty big if/else block instead of a
switch block. Type.GetTypeCode provides a solution which has the best
of both worlds.
 
Thanks, Jon

Yes, I must use the double cast for this to work.

I will use Type.GetTypeCode so that the case statement does not have to
evaluate a big long string.

I wish C# supported dynamic runtime casting so that I could cast based on a
calculation instead of a lookup, but the way I work around this issue is
livable...

Thanks for your help

Earl
 
Back
Top