簡體   English   中英

你如何將IDL翻譯成C#?

[英]How do you translate IDL into C#?

例如, 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);
};

背景

在C#中,接口根據定義為空,並且具有許多可能的實現。 在COM中,通常,接口將具有一個實現並定義調用契約,而不是實現契約(如Web服務或CORBA)。 在C#中,接口的實現是特定於.NET的。 在COM中,接口的實現是語言中立的,但是是二進制實現(與SOAP消息相反,它是文本/ XML)。 這個二進制定義一直是COM的批評者和非Windows系統上COM的緩慢(如果有的話)采用(同樣,與Web服務相對)的食物。

對於幾乎所有的IDL命令,C#中都有同等的可能性,盡管並不總是在接口中。 COM的基本接口始終為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包裝器。

更多信息

COM的一個基本的,但非常精彩和徹底的介紹是Don Box的着名書籍Essential COM ,關於IDL我建議Gudgin的Essential IDL

絕對有關COM和.NET的所有知識都寫在全面但略顯臃腫的.NET和.COM完整的互操作性指南中 不是一本書,你將從封面到封面閱讀,但它是很好的參考資料。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM