System.String conversion to System.Char*

  • Thread starter Thread starter LadyEve
  • Start date Start date
L

LadyEve

Hi!

I've got a problem when converting a System.String into a System.Char*!
Problem: Invalid Cast Operation
Here is the source:

Type type = Type.GetType("System.Char*");
String val = "c";
Object o = Convert.ChangeType(val,type);

val can only be a string, because i'm reading it out of an xml-file!
Xml-Source:
<type>System.Char*</type>
<value>c</value>

I hope you can help me! Thank you ;)

Eva
 
Eva,

What do you need a char* for? Anyway, you use the fixed statement for
that

fixed ( char* pc = val ) { ... }



Mattias
 
Hi Mattias!

Thanks for your fast answere! I am developing a programm to store and
load classes/instances/methods... I store values for methods(according
to their MethodInfo)and for testing purpose i tried to construate a
System.String with the constructor String(Char*, int, int)! Creating and
storing works fine, but reloading does not work!
The method to reload a Value looks like this(Xml-Data see above):

private object GenerateValue(XmlElement element)
{
//Get Type
XmlNodeList xnl = element.GetElementsByTagName("type");
Type valType = Type.GetType(xnl.Item(0).InnerText);
//Get Value
xnl = element.GetElementsByTagName("value");
String value = xnl.Item(0).InnerText;
//Generate Value
return Convert.ChangeType(value,valType);
}
 
Thanks for your fast answere! I am developing a programm to store and
load classes/instances/methods...

Any reason you're not using the framework's serialization support for
this?

//Generate Value
return Convert.ChangeType(value,valType);

The Convert class and its ChangeType method is pretty limited in which
types it can handle and what kinds of conversion. You can't expect it
to work for any value you've persisted.



Mattias
 
Any reason you're not using the framework's serialization >support for
this?

Yes, the structure of the programm is too complicated and can't be
simplified to use xml-serialisation!
I already tried it, but it doesn't work out quite properly!

Ok! Thanks for your information! It seems that problem is far more
complicated, than it seemed at first ;) I'll have to work on that ;)

Eva
 
Back
Top