Question

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 )

Was it helpful?

Solution

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.

OTHER TIPS

Check this one out :Struct Pointer Initialization in C#.

Also, from the Microsoft docs regarding structs :Structs can also contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types, although if several such members are required, you should consider making your type a class instead.

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top