项目A几乎没有重载方法来返回XMLNode和MSXML2.IXMLDOMNode但参数不同,如下所示
static public XmlNode XMLNewChildNode(XmlNode oParent, string sName)
{
...
return xmlNode;
}
和
static public MSXML2.IXMLDOMNode XMLNewChildNode(MSXML2.IXMLDOMNode oParent, string sName)
{
...
return ixmldomnode;
}
项目B没有引用interop.MSXML2.dll,当我使用XMLNewChildNode()返回XMLNode时,我收到编译错误。
XmlNode oNode = XMLHelper.XMLNewChildNode2((XmlNode)oDoc, UCMCommonPTLIndep.gsUCP_DDT_ROOT_NODE);
错误:The type 'MSXML2.IXMLDOMNode' is defined in an assembly that is not referenced. You must add a reference to assembly 'Interop.MSXML2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=98ec9cb7b15b7e98'.
在c#中不可能有这样的重载吗?我错过了什么吗?
答案 0 :(得分:1)
“在c#中不可能有这样的重载吗?”
显然,只要这些重载的消费者拥有所需的参考资料。
您应该能够通过为方法赋予不同的名称来解决此问题,但最好是将方法放在不同的类中。这也可以解决您的问题:
public static class XmlNodeHelper
{
static public XmlNode XMLNewChildNode(XmlNode oParent, string sName)
{
...
return xmlNode;
}
}
public static class MSXMLHelper
{
static public MSXML2.IXMLDOMNode XMLNewChildNode(MSXML2.IXMLDOMNode oParent, string sName)
{
...
return ixmldomnode;
}
}