是否可以将C#Enum暴露给COM Interop调用者,如果是,如何?

时间:2022-09-01 18:36:15

I have a managed assembly that gets called via COM Interop. Like a VBScript client, a Perl client, and so on.

我有一个通过COM Interop调用的托管程序集。像VBScript客户端,Perl客户端等。

The classes are decorated with

课程装饰有

[ClassInterface(ClassInterfaceType.AutoDual)]
[GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000E")]
[ComVisible(true)]

Then of course I do the regasm thing, and all the methods work just fine.

然后当然我做了regasm的事情,所有方法都运行得很好。

But there are also enum types in the assembly. I'd like to use symbolic names COM applications, for the enum values.

但是程序集中也有枚举类型。我想使用符号名称COM应用程序,用于枚举值。

How do I expose the enums via COM interop? Do I just need to add these attributes?

如何通过COM互操作公开枚举?我只需要添加这些属性吗?

[GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000E")]
[ComVisible(true)]

And then, how do I reference those symbolic names in VBScript? I don't see the enum types in OleView. (Should I?) I see all the other types in OleView.

然后,我如何在VBScript中引用这些符号名称?我没有在OleView中看到枚举类型。 (我应该吗?)我在OleView中看到了所有其他类型。

3 个解决方案

#1


3  

VBScript and other late-bound clients use IDispatch to call methods on objects. As such, these languages don't have access to type information in the typelib -- they just create an object from a GUID, get an IDispatch pointer back, and start calling methods by name.

VBScript和其他后期绑定客户端使用IDispatch来调用对象上的方法。因此,这些语言无法访问类型库中的类型信息 - 它们只是从GUID创建对象,返回IDispatch指针,并开始按名称调用方法。

I'm unsure of the COM interop part of the question, but even if the enums did show in OleView, you wouldn't be able to use them directly.

我不确定问题的COM互操作部分,但即使枚举确实在OleView中显示,你也无法直接使用它们。

However, if you are able to publish the enums in the typelib, I wrote a tool eons ago that can generate a script file (vbs or js) containing all the enums from a typelib as constants.

但是,如果您能够在类型库中发布枚举,我之前编写了一个工具,它可以生成一个脚本文件(vbs或js),其中包含来自类型库的所有枚举作为常量。

See here: http://www.kontrollbehov.com/tools/tlb2const/

见这里:http://www.kontrollbehov.com/tools/tlb2const/

#2


11  

My (so far only) .NET assembly that I made COM-visible also had an enum type, which showed up just fine in OleView. I had the whole library be COM-visible so

我(迄今为止)只有我发明COM-visible的.NET程序集也有一个枚举类型,在OleView中显示得很好。我让整个库都是COM可见的

[ComVisible(true)]

was not necessary. Is your enum type public?

没有必要。你的枚举类型是公开的吗?

One thing that happened was that the different enumerations were 'prefixed' with 'enum type name'_:

发生的一件事是,不同的枚举与'枚举类型名称'''前缀':

public enum DataType
{
    INT32,
    FLOAT64,
    INT8
}

turned into:

typedef [...]
enum {
    DataType_INT32 = 0,
    DataType_FLOAT64 = 1,
    DataType_INT8 = 2
} DataType;

in the type library.

在类型库中。

#3


0  

I know this is a very old thread, but I'll add my 2 cents for future explorers. When I define an enum in C# I decorate it with [Guid(...), ComVisible(true)] not GuidAttribute.
For example:

我知道这是一个非常古老的主题,但我将为未来的探险家增加2美分。当我在C#中定义一个枚举时,我用[Guid(...),ComVisible(true)]而不是GuidAttribute来装饰它。例如:

[Guid("28637488-C6B3-44b6-8621-867441284B51"), ComVisible(true)]
public enum myEnum
{
    first,
    second,
    third,
    fourth
}

Then the enumeration would be available in VB6 as myEnum_first, myEnum_second and so forth.

然后枚举将在VB6中以myEnum_first,myEnum_second等方式提供。

You can include the assignment of key values in that list as well, so first = 1 would be valid in the enumeration.

您也可以在该列表中包含键值的赋值,因此first = 1在枚举中有效。

#1


3  

VBScript and other late-bound clients use IDispatch to call methods on objects. As such, these languages don't have access to type information in the typelib -- they just create an object from a GUID, get an IDispatch pointer back, and start calling methods by name.

VBScript和其他后期绑定客户端使用IDispatch来调用对象上的方法。因此,这些语言无法访问类型库中的类型信息 - 它们只是从GUID创建对象,返回IDispatch指针,并开始按名称调用方法。

I'm unsure of the COM interop part of the question, but even if the enums did show in OleView, you wouldn't be able to use them directly.

我不确定问题的COM互操作部分,但即使枚举确实在OleView中显示,你也无法直接使用它们。

However, if you are able to publish the enums in the typelib, I wrote a tool eons ago that can generate a script file (vbs or js) containing all the enums from a typelib as constants.

但是,如果您能够在类型库中发布枚举,我之前编写了一个工具,它可以生成一个脚本文件(vbs或js),其中包含来自类型库的所有枚举作为常量。

See here: http://www.kontrollbehov.com/tools/tlb2const/

见这里:http://www.kontrollbehov.com/tools/tlb2const/

#2


11  

My (so far only) .NET assembly that I made COM-visible also had an enum type, which showed up just fine in OleView. I had the whole library be COM-visible so

我(迄今为止)只有我发明COM-visible的.NET程序集也有一个枚举类型,在OleView中显示得很好。我让整个库都是COM可见的

[ComVisible(true)]

was not necessary. Is your enum type public?

没有必要。你的枚举类型是公开的吗?

One thing that happened was that the different enumerations were 'prefixed' with 'enum type name'_:

发生的一件事是,不同的枚举与'枚举类型名称'''前缀':

public enum DataType
{
    INT32,
    FLOAT64,
    INT8
}

turned into:

typedef [...]
enum {
    DataType_INT32 = 0,
    DataType_FLOAT64 = 1,
    DataType_INT8 = 2
} DataType;

in the type library.

在类型库中。

#3


0  

I know this is a very old thread, but I'll add my 2 cents for future explorers. When I define an enum in C# I decorate it with [Guid(...), ComVisible(true)] not GuidAttribute.
For example:

我知道这是一个非常古老的主题,但我将为未来的探险家增加2美分。当我在C#中定义一个枚举时,我用[Guid(...),ComVisible(true)]而不是GuidAttribute来装饰它。例如:

[Guid("28637488-C6B3-44b6-8621-867441284B51"), ComVisible(true)]
public enum myEnum
{
    first,
    second,
    third,
    fourth
}

Then the enumeration would be available in VB6 as myEnum_first, myEnum_second and so forth.

然后枚举将在VB6中以myEnum_first,myEnum_second等方式提供。

You can include the assignment of key values in that list as well, so first = 1 would be valid in the enumeration.

您也可以在该列表中包含键值的赋值,因此first = 1在枚举中有效。