明确实现接口时装箱的成本是多少

时间:2008-12-02 01:54:58

标签: .net xml explicit-interface

显式成员实施的当前指导原则建议:

  • 使用显式成员来近似私有接口实现。 如果您只是出于基础架构原因需要实现一个接口,并且从不期望开发人员直接从该类型调用该接口上的方法,那么明确地实现成员以从公共视图中“隐藏”它们< / em>的
  • 公开另一种方法来访问任何明确实现的成员,允许子类重写。

一个很好的例子是你想要实现IXmlSerializable接口。预计 ReadXml WriteXml 方法将由XmlSerializer调用,通常不会由开发人员直接调用。

当提供一种替代方法来显式访问您希望允许被覆盖的成员时,调用显式实现的成员似乎是有意义的,以避免代码重复。请考虑以下事项:

using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace Demo
{
    /// <summary>
    /// Demonstrates explicit implementation of the IXmlSerializable interface.
    /// </summary>
    [Serializable(), XmlRoot(ElementName = "foo")]
    public class Foo : IXmlSerializable
    {
        //============================================================
        //  IXmlSerializable Implementation
        //============================================================
        #region GetSchema()
        /// <summary>
        /// Returns an <see cref="XmlSchema"/> that describes the XML representation of the object.
        /// </summary>
        /// <returns>
        /// An <see cref="XmlSchema"/> that describes the XML representation of the object that is 
        /// produced by the <see cref="IXmlSerializable.WriteXml(XmlWriter)"/> method and consumed by the <see cref="IXmlSerializable.ReadXml(XmlReader)"/> method.
        /// </returns>
        /// <remarks>This method is reserved and should not be used.</remarks>
        XmlSchema IXmlSerializable.GetSchema()
        {
            return null;
        }
        #endregion

        #region ReadXml(XmlReader reader)
        /// <summary>
        /// Generates an object from its XML representation.
        /// </summary>
        /// <param name="reader">The <see cref="XmlReader"/> stream from which the object is deserialized.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="reader"/> is a <b>null</b> reference (Nothing in Visual Basic).</exception>
        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            // Class state values read from supplied XmlReader
        }
        #endregion

        #region WriteXml(XmlWriter writer)
        /// <summary>
        /// Converts an object into its XML representation.
        /// </summary>
        /// <param name="writer">The <see cref="XmlWriter"/> stream to which the object is serialized.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="writer"/> is a <b>null</b> reference (Nothing in Visual Basic).</exception>
        void IXmlSerializable.WriteXml(XmlWriter writer)
        {
            // Current class state values written using supplied XmlWriter
        }
        #endregion

        //============================================================
        //  Public Methods
        //============================================================
        #region WriteTo(XmlWriter writer)
        /// <summary>
        /// Saves the current <see cref="Foo"/> to the specified <see cref="XmlWriter"/>.
        /// </summary>
        /// <param name="writer">The <see cref="XmlWriter"/> stream to which the <see cref="Foo"/> is serialized.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="writer"/> is a <b>null</b> reference (Nothing in Visual Basic).</exception>
        public void WriteTo(XmlWriter writer)
        {
            writer.WriteStartElement("foo");

            ((IXmlSerializable)this).WriteXml(writer);

            writer.WriteEndElement();
        }
        #endregion
    }
}

我的问题是关于 WriteXml 方法在此实现中的装箱有多昂贵。 ((IXmlSerializable)this).WriteXml(writer)会显着阻碍性能吗?

3 个答案:

答案 0 :(得分:5)

你的例子中没有拳击......它只是一个演员,它在编译时可以解析,所以它根本不会对性能产生任何影响。

编辑:使用ILDASM查看它,接口强制转换将为您提供虚拟方法调用与常规方法调用,但这可以忽略不计(仍然没有涉及装箱)。

编辑2:如果您使用结构而不是类,那么您将获得一个通过界面的框,并且会有更多的性能损失。

答案 1 :(得分:3)

不,将大量数据写入XmlWriter的成本将使拳击成本相形见绌。

拳击包括:

  1. 从GC分配一片内存
  2. 使用正确的类型信息初始化其标题
  3. 将valuetype数据复制到堆内存
  4. 因此,它与对象构造大致相同。如果您写入XmlWriter的单个数据本身不是字符串,则无论如何都要支付此费用,以构造要写入的字符串。

答案 2 :(得分:1)

为什么不让它们都调用一个执行显式实现的接口功能的私有方法?

public void IXmlSerializable.WriteXml( XmlWriter writer )
{
    InternalWriteXml( writer );
}

public void WriteTo(XmlWriter writer)
{
    writer.WriteStartElement("foo");

    InternalWriteXml(writer);

    writer.WriteEndElement();
}

private void InternalWriteXml( XmlWriter writer )
{
    ...
}