Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to interoperate between COM and .NET objects in the development of C # components

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to carry out the interoperation between COM and. Net objects in the development of C # components? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a simpler and easier way.

Development History of C # components

Many problems have been exposed in the early stage of the development of object-oriented technology, one of which is that with the increasing complexity of the system, the classes of the system begin to explode. and the call dependencies between objects can not be loosely coupled with each other at a higher level of abstraction, which leads to the fragility of this source code-based reuse. Component-based development technology solves the above problems well, and it implements abstraction and reuse at the level of components (aggregation of certain functions, which is a collection of classes). During this period, COM (Component Object Model) technology, as an implementation of such a methodology, began to show its unique charm, while Microsoft led the IT industry to push this technology to stability and maturity.

In the late 1990s, with the complexity of business logic processing and the emergence of many personalized requirements, the complexity of software began to increase, and COM technology began to fail in the field of application technology. At this time, we need a more flexible, standards-based technology to support the development of application software. Net as a cross-era significance of application software development platform quietly. .net realizes software reuse at a higher level in a platform-based way (application software depends on a platform, portability depends on the platform).

Problem description

To maintain the interoperability of .NET and COM technologies, Microsoft provides a specialized component to solve this problem, referring to the namespace System.Runtime.InteropService. It does not provide an effective way for .NET to call existing COM components, but also provides a convenience for writing COM components in .NET.

C # component development steps

Step 1 builds .NET components

Create a new .NET component

Set the COM visibility of this .NET component

Set project properties: "generate"-> "register for COM Interop".

Of course, you can also set COM visibility for each interface, and the ComVisibleAttribute class provides such control.

Step 2 defines component interfaces

Each COM component interface has a * GUID, which is specified by the Guid attribute in the definition of the .NET interface.

In this example, I defined 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 implements component interface

The implementation class of each COM component interface also has a * GUID, defined by the Guid attribute.

The definition of a component class in IDL requires a Default (the interface declaration of the default implementation), which is defined by the ComDefaultInterface property in the .NET component.

This is how I implemented the interface in this example, 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

Suppose the name of the .NET component we generate is XMPTemplate.dll, and the access path is C:\ XMPTemplate.dll

Use the Regasm tool to register .NET components

Regasm C:\ XMPTemplate.dll

Add this .NET component to the global assembly cache

Gacutil / I C:\ XMPTemplate.dll

This is the answer to the question on how to interoperate between COM and .NET objects in the development of C# components. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report