How to do type conversion to unknown type?

G

Guest

I want to convert a string (or object) to a primitive type. But I don't know
the type to convert to at run time.

For example l have variable (lets say its an int):
int unknownType = 0;
And a string:
string str = "123";

If I knew the type is int I could have used:
unknownType = Convert.ToInt32(str); or int.Parse(str) etc.

I also could have used the Convert.ChangeType function, but this function
returns an Object and not a primitive type that again need to cast.

Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Demorsy,

Any method that converts one type to another and doesn't spellout the the
type in its name (ChangeType vs. ToInt32) would return Object.

However the actuall type of the object in memory would be the type you
converted to.

I don't see any possible situation where the situatuation you describe could
happen, though.

Maybe I miss something but if you have:
int unknownType = 0;
And a string:
string str = "123";

You have to put *int* infront of unknownType otherwise you cannot compile.
Constructions like:
??? unknowType; are not acceptable in C#.

Unlike javascript for example where you decalre variables like:
var unknowType;
where you don't specify the type are not allowed in C#.

In all .NET languages If you don't want to (or can't) specify the type you
declare the variable of type Object: in this case you shouldn't have
problems using ChangeType method. But that also means you that don't want to
use anything more from the type than the Object class interface provides.



Stoitcho Goutsev (100) [C# MVP]
 
G

Guest

I'm not the one that create the instance of the unknownType.
Actually I'm receiving an XML string that contain values to be set on a real
variable that are located in a DataSet.
I do not know the variable type at development time, but I can know it at
run time using the GetType() method. So now I know the type and I need to set
its value as found in the XML string.

So you see, I need to convert a string to an unknown type.

The .NET should support that, yet I did not find how to do that.

Any idea on how to do that?
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Demorsy,

I think I see now what your problem is. Unfortunately, AFAIK there is no
conversion that can help you with this. As I said all generic data
conversion methods that could exist would have return type Object.

When you populate the data table you need to create and add a DataRow. Even
if you have typed DataRow it inherits from the DataRow class untyped indexer
is still available.

I believe you can do.

myDataRow["some column name"] = Convert.ChangeType(value, <xxxx>.GetType());

HTH
Stoitcho Goutsev (100) [C# MVP]
 
J

Jonathan Allen

Just build a case structure around the type.

if getType == String
// soem code
else if getType == Int32
// other code
 
S

Stoitcho Goutsev \(100\) [C# MVP]

The question was:

"Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?"


--
Stoitcho Goutsev (100) [C# MVP]

Jonathan Allen said:
Just build a case structure around the type.

if getType == String
// soem code
else if getType == Int32
// other code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top