WHAT IS MANAGED WAY TO USE A VARIABLE'S POINTER IN STRUCTURE?

Monaj Singh

Total Post:41

Points:287
Posted by  Monaj Singh
C#
 924  View(s)
Ratings:
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;

 

            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 ?

Thanks

  1. sandy taylor

    Post:46

    Points:324
    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 Nov-04-2014 02:32:19 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!