Sgen.exe 中的错误

发布于 2024-07-29 11:24:54 字数 1191 浏览 3 评论 0原文

看来 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷到爆炸 2024-08-05 11:24:54

不,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文