i'm trying to write a generic function which should be able to parse a xml file
here is the code
public struct XmlArg
{
public string Name;
public Type T;
public object Value;
};
static bool ParseXmlArgs(XmlReader xml, params XmlArg[] args)
{
for (int i = 0; i < args.Length; ++i)
{
if (xml.MoveToContent() != XmlNodeType.Element || xml.Name != args[i].Name)
{
return false;
}
args[i].Value = xml.ReadElementContentAs(args[i].T, null);
}
return true;
}
static void Main(string[] args)
{
int a = 0;
ParseXmlArgs(
XmlTextReader.Create("C:\\Users\\Yazilim\\Desktop\\XML.xml"),
new XmlArg[]{
new XmlArg() { Name = "ErrorCode", T = typeof(int), Value = a}});
}
i know that i should pass a's pointer to Value ( it's type should be some different type other than object of course )
but i don't want it to be non-managed way.
is there any managed way to use a variable's pointer in structure ?
( the function may be wrong or incorrect, and it's not the point )
Assuming that you want XmlArg.Value
to point to a
instead of just clone its value, then the answer is just "You can't". There is no address-of operator in C#, and that is for good reasons so, because you could create a reference to a variable on the stack (like your a is). When you run out of scope, that variable contains garbage and then ZONK.