المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Explicit casting with Type variable



C# Programming
01-28-2010, 01:50 PM
Is it possible to make a explicit casting using a variable?

I'm passig a boxed object, and it's Type, as arguments. Inside the method I would like to unbox and use the content

private void myMethod ( Object boxedObject, Type dataType)
{
....
... = (dataType)boxedObject;
....
}

I know it doesn't compile this way. By the moment i'm using this kind of code:

switch (dataType.ToString())
{
case "int":
... = (int)boxedObject;
case "string"
... = (string)boxedObject;
...
}

But... is there a way to make something similar to "(dataType)boxedObjec)" that doesn't need using 'if' or 'case' for every dataType?

Thanks!