views:

396

answers:

3

I have this XML document

<AdditionalParameters>
<PublishToPdf Type ="System.Boolean">False</PublishToPdf>
</AdditionalParameters>

in my code and I'm trying to build an array of arguments containing the <PublishToPdf> node.

object test = (object) ((typeof(publishNode.Attributes["Type"].value)) publishNode.InnerText);

This breaks at compile time of course. I can't figure out how to cast the publishNode.InnerText('false') to a runtime defined object of type specified in the XML file and store it in an object (which will conserve the type).

I've been searching for more than an hour, and I've seen such complex things and each different from the other. I can't tell what specifically is my problem.

+1  A: 

You can't do exactly what you're trying to do. First, the typeof keyword does not allow for dynamic evaluation at runtime. There are means by which to do this using reflection, with methods like Type.GetType(string), but the Type objects returned from these reflective functions can't be used for operations like casting.

What you need to do is provide a means of converting your type to and from a string representation. There is no automatic conversion from any arbitrary type. For your example, you can use bool.Parse or bool.TryParse, but those are specific to the bool type. There are similar methods on most primitive types.

Adam Robinson
i tried to make the enclosed code bold, but instead it just inserted asterisks. Thanks for John kugelman for the edit.Now, ok, forget about typeof, i also tried Type.GetType() it also failed.And i agree with you about a factory that would create an object of type T from the publishNode.InnerText using the type attribute. But my problem is that the type eclosed in the attribute, is generic, i don't know at runtime what will be. So should my factory have cases of all Framework types(forget about other types)?or there's another way?
LolaRun
+3  A: 

You can use Convert.ChangeType :

object value = Convert.ChangeType(stringValue, destinationType);
Thomas Levesque
Thanks a lot, it worked fine.
LolaRun
Yes, and to get destinationType, use var destinationType = Type.Parse(publishNode.Attributes["Type"].value);
Digitalex
A: 

The simple solution, assuming there is a limited number of possible types;

object GetValueObject(string type, string value)
{
  switch (type)
  {
    case "System.Boolean":
      return Boolean.Parse(value);
    case "System.Int32":
      return Int32.Parse(value);
    ...
    default:
      return value;
  }
}  

var type = publishNode.Attributes["Type"].value;
var value = publishNode.InnerText;
var valueObject = GetValueObject(type, value);
Digitalex
i'm working with .net framework 2.0. and i don't have var.And the solution you suggested is the factory implementing the creation of objets of all types, which are countless.But thanks for your help anyway.
LolaRun
Well, the var was just to make the code shorter, u can use string, string, object for these three instead. And, as I said, if you only need to support a limited number of types, this would work fine. If not, then you're right to say it's not practical.
Digitalex