I’ve got an object[] array which contains some mixture of builtin types, like Int/Byte/String, and I’m trying to cast at runtime from object to the correct type, then handing that casted data to a 3rd party library that has Write(builtin type overloads) method.
I thought I could just do Obj.Write((expression yielding System.Type)objArg), but it errors on me.
Here’s a code sample that explains it better:
object testData = (int)42;
int final = (GetAType())testData;
private Type GetAType()
{
return typeof(Int32);
}
Any suggestions?
Alternate ways to accomplish this would be helpful also.
For some context, here’s my original problem.
3rd party library with a bunch of overloads for different types.
3rdPartyLibrary.Write(bool source)
3rdPartyLibrary.Write(int16 source)
3rdPartyLibrary.Write(int32 source)
3rdPartyLibrary.Write(string source)
I’m trying to abstract a layer between 3rdPartyLibrary and the rest of my code
Such that I can have
Object[int32]
Object[int16]
Object[string]
Object[int32]
I’m trying to do something simple seeming, like this (psuedo-coded)
loop Object[]
{
3rdPartyLibrary.Write( (object[i].GetType()) object[i] )
}
I’ve been reading all the similar sounding question, it looks like this may not be possible?
What you are looking for would be pointless, as the result still would have to have a single type for all operations, and that would have to be
objectto handle all the different types.Just use a switch for the types that you support: