Sgen.exe 中的错误
看来 sgen.exe 无法生成泛型类型 XmlSerializer,对吗?
我的通用类型:
[Serializable]
[XmlRoot(ElementName = "Masterx")]
public class Masterx<T> where T : class, new()
{....}
序列化器代码:
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;
}
运行 sgen.exe 并使用“Reflector”检查生成的程序集后,我发现生成的程序集不包含 MasterxXmlSerializer 类,为什么?
有人有同样的经历吗? 如何修复它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,
sgen
不会为开放泛型类型生成序列化器。 作为 XmlSerializer 的一般规则,如果您没有通过静态分析获得整个架构,则预生成的程序集不会有任何帮助。因此,如果使用泛型,则需要在代码中定义所有派生子类才能使用预生成的程序集。
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.