1. Overview
Microsoft has done a lot of work in solving interoperability with the previous COM and SDK development technologies, including COM and. net objects and.. net. This article attempts to explore the implementation and related technologies of interoperability between COM and. Net objects in a practical way.
2. Development History
At the beginning of the development of object-oriented technology, many problems were exposed. One of them was that as the complexity of the system continued to rise, the class of the system began to explode, in addition, call dependencies between objects cannot be loosely coupled to each other at a higher abstraction level, which leads to the vulnerability of source code-based reuse. The component-based development technology solves the preceding problems well. It implements abstraction and reuse at the component level (the aggregation of a function is a collection of classes. In this period, the COM (Component Object Model) technology, as a method of implementation, began to show its unique charm, microsoft leads the IT industry to push this technology to a stable and mature level.
By the end of 1990s, with the complexity of business logic processing and the emergence of many Personalized Requirements, the software complexity began to increase, and the COM technology began to become insufficient in the field of application technology. At this time, we need a more flexible and standard-based technology to support the development of application software. NET is quietly approaching as a development platform for application software with cross-era significance .. Net is a platform-based approach (applications depend on a platform, and portability depends on the platform), and software reuse is realized at a higher level.
3. Problem Description
To maintain the interoperability between. NET and COM technologies, Microsoft provides a dedicated component to solve this problem. For details, refer to the namespace system. runtime. interopservice. It does not provide an effective method for. Net to call the original COM component, but also provides a convenient way to compile COM components using. net.
4. Steps
Step1. construct the. NET Component and set its COM component visibility.
Step2. define the COM Component Interface
Step3. implement COM Component Interface
Step 4. Deployment
Step 5. Test
Step 1 build. NET components
N create a. NET component, such:
N sets the com visibility of this. NET component.
Set project properties: "generate"-> "register for com InterOP ".
Of course, you can also set the com visibility for each interface. The comvisibleattribute class provides such control.
Step 2 define component interfaces
Each COM Component Interface has a unique guid, which is specified by the guid attribute in the. NET interface definition.
In this example, I define the following interface:
[Typelibtype (4160)]
[GUID ("F0485D81-59C1-44b1-9316-D474E26C026E")]
Public interface ixmpreader
{
[Dispid (0)]
String metadata {Get; set ;}
[Dispid (1)]
Ixmptemplate readtemplate (string XPath );
}
Step 3 implement the Component Interface
Each implementation class of the COM Component Interface also has a unique guid, which is defined by the guid attribute,
The definition of component classes in IDL requires a default (default implemented interface declaration), which is defined by the comdefaultinterface attribute in the. NET component.
In this example, I implemented this interface as follows:
Namespace xmptemplate
{
[Typelibtype (4160)]
[GUID ("AD665240-9E4D-4c30-9475-023EA44E41BD")]
[Comdefaultinterface (typeof (ixmpreader)]
[Comsourceinterfaces (typeof (ixmpreader)]
Public class xmpreader: ixmpreader
{
Private string _ metadata = "";
Public String metadata
{
Get {return _ metadata ;}
Set {_ metadata = value ;;}
}
Public ixmptemplate readtemplate (string XPath)
{
Xmldocument document = new xmldocument ();
Document. loadxml (metadata );
Xmlnode node = Document. selectsinglenode (XPath );
Xmptemplate template = new xmptemplate ();
Template. loadxml (node. innerxml );
Return template;
}
}
Step 4 Deployment
Assume that the generated. NET component is named xmptemplate. dll and the access path is c: \ xmptemplate. dll.
N use regasm to register. NET components
Regasm c: \ xmptemplate. dll
N add this. NET component to the Global Assembly Cache
Gacutil/I c: \ xmptemplate. dll
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/soudog/archive/2007/04/30/1593346.aspx