views:

181

answers:

1
+1  Q: 

Bug in Sgen.exe

It seems that sgen.exe could not generate generic type XmlSerializer, right?
My genereic type:

[Serializable]
[XmlRoot(ElementName = "Masterx")]
public class Masterx<T> where T : class, new()
{....}

Serializer code:

 protected virtual List<T> ParseXMLToObject<T>(string resultXML) where T : class, new()
    {
        //return ParseXMLToObject<T>(resultXML, "Masterx");
        XmlSerializer xs = new XmlSerializer(typeof(Masterx<T>));
        System.IO.StringReader sr = new System.IO.StringReader(resultXML);
        XmlReader xr = XmlReader.Create(sr);

        Masterx<T> masterx = null;
        if (!string.IsNullOrEmpty(resultXML))
        {
            if (xs.CanDeserialize(xr))
            {
                //Parse the xml to object
                masterx = xs.Deserialize(xr) as Masterx<T>;
            }
        }
        List<T> rtnObjList = new List<T>();
        if (masterx != null)
        {
            rtnObjList = masterx.MasterxRowList;
        }
        return rtnObjList;
    }

After run sgen.exe and check generated assembly by using "Reflector", I found that generated assembly didn't contain MasterxXmlSerializer class, why?

Does someone have same experience? How to fix it?

A: 

No, sgen doesn't generate serializers for open generic types. As a general rule with XmlSerializer, pre-generated assemblies can't be helpful if you don't have the whole schema available through static analysis.

Thus, if you use generics, you need to define all the derived subclasses in code to be able to use pre-generated assemblies.

Yacoder