4

例如,DOM 规范有各种IDL 定义接口节点就是其中之一。您将如何将其(甚至其中的一部分)翻译成实际的 C#?我的意思是,你甚至会从哪里开始?据我了解,C# 的接口的行为与 IDL 在这里调用的接口大不相同。我错了吗?

interface Node {

  // NodeType
  const unsigned short      ELEMENT_NODE                   = 1;
  const unsigned short      ATTRIBUTE_NODE                 = 2;
  const unsigned short      TEXT_NODE                      = 3;
  const unsigned short      CDATA_SECTION_NODE             = 4;
  const unsigned short      ENTITY_REFERENCE_NODE          = 5;
  const unsigned short      ENTITY_NODE                    = 6;
  const unsigned short      PROCESSING_INSTRUCTION_NODE    = 7;
  const unsigned short      COMMENT_NODE                   = 8;
  const unsigned short      DOCUMENT_NODE                  = 9;
  const unsigned short      DOCUMENT_TYPE_NODE             = 10;
  const unsigned short      DOCUMENT_FRAGMENT_NODE         = 11;
  const unsigned short      NOTATION_NODE                  = 12;

  readonly attribute DOMString       nodeName;
           attribute DOMString       nodeValue;
                                        // raises(DOMException) on setting
                                        // raises(DOMException) on retrieval

  readonly attribute unsigned short  nodeType;
  readonly attribute Node            parentNode;
  readonly attribute NodeList        childNodes;
  readonly attribute Node            firstChild;
  readonly attribute Node            lastChild;
  readonly attribute Node            previousSibling;
  readonly attribute Node            nextSibling;
  readonly attribute NamedNodeMap    attributes;
  // Modified in DOM Level 2:
  readonly attribute Document        ownerDocument;
  // Modified in DOM Level 3:
  Node               insertBefore(in Node newChild, 
                                  in Node refChild)
                                        raises(DOMException);
  // Modified in DOM Level 3:
  Node               replaceChild(in Node newChild, 
                                  in Node oldChild)
                                        raises(DOMException);
  // Modified in DOM Level 3:
  Node               removeChild(in Node oldChild)
                                        raises(DOMException);
  // Modified in DOM Level 3:
  Node               appendChild(in Node newChild)
                                        raises(DOMException);
  boolean            hasChildNodes();
  Node               cloneNode(in boolean deep);
  // Modified in DOM Level 3:
  void               normalize();
  // Introduced in DOM Level 2:
  boolean            isSupported(in DOMString feature, 
                                 in DOMString version);
  // Introduced in DOM Level 2:
  readonly attribute DOMString       namespaceURI;
  // Introduced in DOM Level 2:
           attribute DOMString       prefix;
                                        // raises(DOMException) on setting

  // Introduced in DOM Level 2:
  readonly attribute DOMString       localName;
  // Introduced in DOM Level 2:
  boolean            hasAttributes();
  // Introduced in DOM Level 3:
  readonly attribute DOMString       baseURI;

  // DocumentPosition
  const unsigned short      DOCUMENT_POSITION_DISCONNECTED = 0x01;
  const unsigned short      DOCUMENT_POSITION_PRECEDING    = 0x02;
  const unsigned short      DOCUMENT_POSITION_FOLLOWING    = 0x04;
  const unsigned short      DOCUMENT_POSITION_CONTAINS     = 0x08;
  const unsigned short      DOCUMENT_POSITION_CONTAINED_BY = 0x10;
  const unsigned short      DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0x20;

  // Introduced in DOM Level 3:
  unsigned short     compareDocumentPosition(in Node other)
                                        raises(DOMException);
  // Introduced in DOM Level 3:
           attribute DOMString       textContent;
                                        // raises(DOMException) on setting
                                        // raises(DOMException) on retrieval

  // Introduced in DOM Level 3:
  boolean            isSameNode(in Node other);
  // Introduced in DOM Level 3:
  DOMString          lookupPrefix(in DOMString namespaceURI);
  // Introduced in DOM Level 3:
  boolean            isDefaultNamespace(in DOMString namespaceURI);
  // Introduced in DOM Level 3:
  DOMString          lookupNamespaceURI(in DOMString prefix);
  // Introduced in DOM Level 3:
  boolean            isEqualNode(in Node arg);
  // Introduced in DOM Level 3:
  DOMObject          getFeature(in DOMString feature, 
                                in DOMString version);
  // Introduced in DOM Level 3:
  DOMUserData        setUserData(in DOMString key, 
                                 in DOMUserData data, 
                                 in UserDataHandler handler);
  // Introduced in DOM Level 3:
  DOMUserData        getUserData(in DOMString key);
};
4

1 回答 1

6

背景

在 C# 中,根据定义,接口是空的,并且有许多可能的实现。在 COM 中,一般来说,一个接口将有一个实现并定义一个调用契约,而不是实现契约(如 Web 服务或 CORBA)。在 C# 中,接口的实现是 .NET 特定的。在 COM 中,接口的实现与语言无关,而是二进制实现(与 SOAP 消息相反,SOAP 消息是文本/XML)。这种二进制定义一直是 COM 批评者的食粮,以及在非 Windows 系统上缓慢(如果有的话)采用 COM(同样,与 Web 服务相反)。

对于几乎所有 IDL 命令,在 C# 中都有相同的可能性,尽管并不总是在界面中。COM 的基接口 always IUknown,用于对象引用计数,所有 COM 对象都必须包含该接口。

在演讲中,当谈论 COM 接口时,您通常会谈论实现。在 c# 中,您通常谈论调用者和实现者理解的空契约。

翻译 IDL

正如您所提到的,上面的 IDL 是针对 DOM 的。DOM 是用 C# 实现的,比它的 COM 表亲(和许多版本)强大得多。如果你真的想创建一个可以调用 COM DOM 接口的 C# 类包装器:

  • 使用 MIDL(Visual Studio 自带)创建 TLB,
  • 然后运行 ​​tlbimp.exe(与 .NET 一起提供)以创建一个 .NET COM 包装器,您可以将其包含在您的项目中。

您还可以直接在 COM dll(进程中)或可执行文件(进程外)上运行 tlbimp.exe,以创建 .NET COM Wrapper。

更多信息

对 COM 的基本但极其出色和彻底的介绍是 Don Box 的著名书籍Essential COM,关于 IDL 我建议Gudgin 的 Essential IDL

绝对所有关于 COM 和 .NET 的知识都写在Nathan撰写的全面但略显臃肿的.NET 和 .COM The Complete Interoperability Guide中。不是一本你会从头到尾阅读的书,但它是很好的参考资料。

于 2010-07-24T14:01:48.657 回答