precompile XmlSerializers与XmlAttributeOverrides [英] Precompile XmlSerializers with XmlAttributeOverrides

查看:166
本文介绍了precompile XmlSerializers与XmlAttributeOverrides的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中构建XmlSerializer的实例,是动态生成的序列化和反序列化指定类型的组件。这是一个耗时的过程。从微软的sgen.exe工具可用于precompile XmlSerializer的实例以后使用它们而不动态生成它们。不幸的是,这是不可能的,它们使用XmlAttributeOverrides XmlSerializer的实例。

When constructing XmlSerializer instances in .NET, assemblies for serializing and deserializing the specified type are generated dynamically. This is a time-consuming process. The sgen.exe tool from Microsoft can be used to precompile XmlSerializer instances to use them later without generating them dynamically. Unfortunately this is not possible with XmlSerializer instances which use XmlAttributeOverrides.

有没有什么办法precompile这些XmlSerializer的实例,以避免在运行时产生?

Is there any way to precompile these XmlSerializer instances to avoid generation at run-time?

推荐答案

安德烈亚斯,这不是SGEN工具本身的问题,这是由于XmlSerializer的implementaion。

Andreas, this is not a problem of the sgen tool itself, this is due to the XmlSerializer implementaion.

在创建XmlSerializer的实例,使用构造函数只有一个类型的参数,它会和检查缓存并查找pre-生成的程序集。 但是,当你使用构造与XmlAttributeOverrides,XmlSerializer的不检查任何缓存,去生成临时组装的时候了。

When you create an instance of XmlSerializer, using the constructor with only one Type argument, it goes and checks the cache and looks for the pre-generated assemblies. But when you use the constructor with XmlAttributeOverrides, XmlSerializer doesn't check any caches and goes generating the temp assembly right away.

最可能的是,这是因为在序列化逻辑的相当激进的变化,你可以achive使用XmlAttributeOverrides说法,不能预见,在编译时通过工具,如SGEN。

Most probably, that's because of the quite radical changes in the serialization logic that you can achive using the XmlAttributeOverrides argument, which cannot be "foreseen" in compile-time by tools like sgen.

如果你需要有东西pre-编译,你[叹气]必须避免的XmlAttributeOverrides。如果这是不可能的,尽量提前-的时间创建所需XmlSerializer的情况下,也许在后台线程。

If you need to have things pre-compiled, you [sigh] have to avoid the XmlAttributeOverrides. If this is not possible, try creating the required XmlSerializer instances ahead-of-time, maybe in a background thread.

只要您有兴趣,这里是$ C $下的默认构造函数(检查缓存,并试图找到一个pre-生成的程序集):

Just for your interest, here is the code for the default constructor (checks cache and tries to find a pre-generated assembly):

public XmlSerializer(Type type, string defaultNamespace)
{
    this.events = new XmlDeserializationEvents();
    if (type == null)
    {
        throw new ArgumentNullException("type");
    }
    this.mapping = GetKnownMapping(type, defaultNamespace);
    if (this.mapping != null)
    {
        this.primitiveType = type;
    }
    else
    {
        this.tempAssembly = cache[defaultNamespace, type];
        if (this.tempAssembly == null)
        {
            lock (cache)
            {
                this.tempAssembly = cache[defaultNamespace, type];
                if (this.tempAssembly == null)
                {
                    XmlSerializerImplementation implementation;
                    Assembly assembly = TempAssembly.LoadGeneratedAssembly(type, defaultNamespace, out implementation);
                    if (assembly == null)
                    {
                        this.mapping = new XmlReflectionImporter(defaultNamespace).ImportTypeMapping(type, null, defaultNamespace);
                        this.tempAssembly = GenerateTempAssembly(this.mapping, type, defaultNamespace);
                    }
                    else
                    {
                        this.mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
                        this.tempAssembly = new TempAssembly(new XmlMapping[] { this.mapping }, assembly, implementation);
                    }
                }
                cache.Add(defaultNamespace, type, this.tempAssembly);
            }
        }
        if (this.mapping == null)
        {
            this.mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
        }
    }
}

这里是XmlAttributeOverrides(始终生成序列化程序集)中使用的构造函数:

And here is the constructor used with XmlAttributeOverrides (always generates serialization assembly):

public XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace, string location, Evidence evidence)
{
    this.events = new XmlDeserializationEvents();
    if (type == null)
    {
        throw new ArgumentNullException("type");
    }
    XmlReflectionImporter importer = new XmlReflectionImporter(overrides, defaultNamespace);
    for (int i = 0; i < extraTypes.Length; i++)
    {
        importer.IncludeType(extraTypes[i]);
    }
    this.mapping = importer.ImportTypeMapping(type, root, defaultNamespace);
    if (location != null)
    {
        this.DemandForUserLocation();
    }
    this.tempAssembly = GenerateTempAssembly(this.mapping, type, defaultNamespace, location, evidence);
}

这篇关于precompile XmlSerializers与XmlAttributeOverrides的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆