what is managed way to use a variable's pointer in structure?

Total Post:41

 1311  View(s)
Rate this:

Hi Guys

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;
                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 ?


  1. Post:46

    Re: what is managed way to use a variable's pointer in structure?

    create an array, pass it to a method, and then read its contents afterwards:

    You need to construct the XmlArg[] array before calling the method, and forget about the a variable until the parsing if finished:

    var reader = XmlTextReader.Create("C:\\Users\\Yazilim\\Desktop\\XML.xml");
    var results = new []
        new XmlArg() { Name = "ErrorCode", T = typeof(int) },
        new XmlArg() { Name = "ErrorMessage", T = typeof(string) }
    if (ParseXmlArgs(reader, results) == true)
        // results were passed by reference, so do something with these values

      Modified On Apr-06-2018 03:56:31 AM