简体   繁体   English

导出 COM 可见 .NET 类的方法别名

[英]Export a method alias of COM visible .NET class

I have a construct of a .NET implementation of some utility and a .WSC script that instantiates the .NET object.我有一些实用程序的 .NET 实现的构造和一个实例化 .NET 对象的.WSC脚本。 The goal is to eliminate the WSC script and to instantiate and call the .NET implementation directly.目标是消除WSC脚本并直接实例化和调用 .NET 实现。 But this discovered a naming problem.但这发现了一个命名问题。

The class defined in the WSC contains several methods. WSC 中定义的类包含几个方法。 One of these methods is Finalize() .其中一种方法是Finalize() The function name is reserved in .NET since it is used for the life time and GC management.函数名称在 .NET 中保留,因为它用于生命周期和 GC 管理。 Therefore the WSC calls a .NET function with a different name:因此, WSC使用不同的名称调用 .NET 函数:

<?xml version="1.0"?>
<component>
  <public>
    <method name="Finalize"/>
  </public>
  <object id="wrapper" progid="JS.Wrapper"/>

  <script language="JScript">
    <![CDATA[
      // Processes last required steps and closes all logs
      function Finalize() {
         return wrapper.Finish();
      }
      // Any other function
      function Foo() {
        wrapper.Foo();
      }
    ]]>
  </script>
</component>

The .NET class implements the Finish function: .NET 类实现了Finish函数:

[ComVisible(true)]
[ProgId("JS.Wrapper")]
public class MyCom
{
    // Processes last required steps and closes all logs
    public void Finish()
    {
    }

    public void Foo()
    {
    }
}

There are thousands of scripts that call all the Foo() and Bar() functions.有数以千计的脚本调用了所有的 Foo() 和 Bar() 函数。 And most of them also calls the Finalize function.而且他们中的大多数还调用了Finalize函数。 Therefore it is impossible to change the function name at the COM interface.因此无法在 COM 接口更改函数名称。

  • How can the function Finish be made COM-Visible with the name Finalize ?如何将函数 Finish 设为 COM-Visible 并命名为Finalize
  • Is there a way to implement two Finalize functions, one for .NET run-time and one as exported at the COM interface.有没有办法实现两个 Finalize 函数,一个用于 .NET 运行时,一个用于在 COM 接口导出。
  • Is it possible to implement a COM interface explicitly, ie make a custom implementation of the COM interface?是否可以显式实现 COM 接口,即自定义 COM 接口的实现?

There is an attribute ComAliasName that can be used to make an property, indexer, field, param, or return visible with a an alias.有一个属性ComAliasName可用于使用别名使属性、索引器、字段、参数或返回可见。 But it can't be used for a method name.但它不能用于方法名称。 How can a method made visible as an alias?如何使方法作为别名可见?

The method Finalize can be exposed by defining explicitly the COM interface.可以通过显式定义 COM 接口来公开Finalize方法。

public interface IMyCom
{
    // Processes last required steps and closes all logs
    void Finalize();
    void Foo();
}

It's important to choose the ClassInterfaceType.None in the implementing .NET class.在实现 .NET 类中选择ClassInterfaceType.None很重要。 The implementation in the .NET class should specify the interface that is implemented. .NET 类中的实现应指定实现的接口。 With the scope prefix IMyCom the compiler can distinguish between the name of the method at the COM interface and some .NET CLR internal names.使用范围前缀IMyCom ,编译器可以区分 COM 接口上的方法名称和某些 .NET CLR 内部名称。 The makes it unnecessary to use a different method name and to create an alias.这使得不必使用不同的方法名称和创建别名。

[ComVisible(true)]
[ProgId("JS.Wrapper")]
[ClassInterface(ClassInterfaceType.None)]
public class MyCom : IMyCom
{
    // Explicitly specify the scope of the Finalize method.
    void IMyCom.Finalize()
    {
    }

    void IMyCom.Foo()
    {
    }
}

This approach makes it also clear that this is the implementation of a COM interface method.这种方法也清楚地表明这是一个 COM 接口方法的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM