gpt4 book ai didi

.net - 显式实现接口(interface)时 coSTLy 是如何装箱的

转载 作者:数据小太阳 更新时间:2023-10-29 02:49:35 26 4
gpt4 key购买 nike

当前的显式成员实现指南建议:

  • 使用显式成员来近似私有(private)接口(interface)实现。 如果您仅出于基础架构原因需要实现一个接口(interface),并且您从不期望开发人员直接从该类型调用该接口(interface)上的方法,那么显式实现成员以从公众 View 中“隐藏”它们.
  • 公开一种替代方法来访问允许子类覆盖的任何显式实现的成员。

一个很好的例子就是当你想实现 IXmlSerializable界面。 ReadXmlWriteXml 方法应由 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) 会显着影响性能吗?

最佳答案

您的示例中没有装箱...它只是一个转换,并且在编译时可解析,因此它根本不会对性能产生任何影响。

编辑:用 ILDASM 来看,接口(interface)转换会给你一个虚拟方法调用而不是一个常规方法调用,但这可以忽略不计(仍然没有涉及装箱)。

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

关于.net - 显式实现接口(interface)时 coSTLy 是如何装箱的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/332803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com