jeudi 1 septembre 2016

SetMethod in Reflection is no supported for .net 4.0

I have a piece of code that works in .Net 4.5. However, we discovered that some of the machines that we are trying to support only have .Net 4.0 on win 2003 32bit with no chance of being able to upgrade to anything else. When I compile with Net 4.0 (x86), it says that there is no such a thing as SetMethod in PropertyInfo class. The doc on SetMethod says that is supported since .net 4.5.

Here, I'm trying to read the xml nodes and set the public properties of a an object. I first check to see what type is the data and base on that, I do my casting.

How can I make the same logic work without the SetMethod or do it in a 4.0oee-way?

            RealDataWebRef.RealDataResponse rd = new RealDataWebRef.RealDataResponse();


        List<string> chng = new List<string>();


        foreach (XmlNode xn in xdoc)
        {

            foreach (XmlNode x in xn)
            {
                Type myType = rd.GetType();
                PropertyInfo pinfo = myType.GetProperty(x.Name);

                if (pinfo.SetMethod.ToString().Contains("Int32"))
                {
                    int v;
                    try
                    {
                        v = int.Parse(x.InnerText);
                    }
                    catch (Exception ex)
                    {
                        v = -1;
                        rd.DQ_HAS_ERROR = true;
                        rd.DQ_ERR_STR += "Error in " + x.Name + ":" + ex.ToString() + Environment.NewLine;
                        Console.WriteLine(rd.DQ_ERR_STR);
                    }
                    pinfo.SetValue(rd, v, null);
                    chng.Add(x.Name);

                } else if (pinfo.SetMethod.ToString().Contains("DateTime"))
      blah blah





Aucun commentaire:

Enregistrer un commentaire