从 C# 创建 COM 索引属性?

发布于 2024-11-05 16:09:55 字数 285 浏览 0 评论 0原文

我正在尝试用新的 .net DLL 来模仿旧的 vb6 dll。模仿必须是完美的,以便调用者不知道他们正在使用新的 .dll。

不过我有一个好奇心。在 VB6 中,它在对象库中具有以下内容:

Property BankList(Index As Long) As String

但是据我所知,这不能在 .net 中完成?

我能得到的最接近的是创建一个展示该行为的函数,但随后 COM 类型从属性变为方法。

谁能建议我如何使用属性创建该签名?

I am trying to mimic an old vb6 dll with a new .net one. The mimicry has to be perfect so that that callers don't know they are using a new .dll.

I have a curiousness though. In VB6 it has the following in the object library:

Property BankList(Index As Long) As String

But AFAIK property this can't be done in .net?

The closest I can get is creating a function that exhibits that behaviour but then the COM type goes from Property to Method.

Can anyone suggest how I would create that signature with a property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苍景流年 2024-11-12 16:09:55

您可以使用 IndexerNameAttribute 属性向其他语言公开它的名称。不过,我不确定这是否能实现您的目标。

不幸的是,C# 仅支持调用命名索引器作为 COM 互操作的一部分,没有受支持的语言方式来实现您自己的(即,类只能具有 默认索引器,带有 IndexerNameAttribute 属性)。

您可以通过使用索引器实现一个类型,然后拥有该类型的属性,来创建与 C# 调用方看起来类似的内容,但它并不完全映射到您需要的 VB6 等效项。

另请参阅:使用索引器 (C#)

旁白
正如其他答案中提到的,虽然 C# 不支持命名索引器,但 .NET CLR 和其他一些语言(例如 VB.NET)支持命名索引器。您可能需要考虑更改目标语言才能获得此功能。

You can adorn a regular indexer with the IndexerNameAttribute attribute to expose a name for it to other languages. I'm not sure if this will achieve your goal, though.

Unfortunately, C# only supports the calling of named indexers as part of COM interop, there is no supported language way of implementing your own (i.e., a class can only have the default indexer with an IndexerNameAttribute attribute).

You can create something that looks similar for C# callers by implementing a type with an indexer and then having a property of that type, but it doesn't map exactly to the VB6 equivalent you need.

See also: Using Indexers (C#)

Aside
As has been mentioned in other answers, while C# doesn't support named indexers, the .NET CLR and some other languages, such as VB.NET, do. You may want to consider changing your target language in order to get this feature.

忆伤 2024-11-12 16:09:55

根据 http ://blogs.msdn.com/b/kirillosenkov/archive/2009/10/20/indexed-properties-in-c-4-0.aspx无法在 C# 中声明索引属性。然而,与其他一些答案所述相反,CLR确实支持它们,并且您可以在 VB.NET 中声明它们。

According to http://blogs.msdn.com/b/kirillosenkov/archive/2009/10/20/indexed-properties-in-c-4-0.aspx you can't declare indexed properties in C#. However, in contrast to what some of the other answers state, the CLR does support them, and you can declare them in VB.NET.

来日方长 2024-11-12 16:09:55

无法在 C# 中创建命名参数化属性(只有一个名为 this 的默认属性可用)。

有许多选项:

  • 更改接口(但这没有抓住要点,因为客户端代码需要更改)。
  • 使用VB (.net),它可以创建此类属性。
  • 使用 C++ 创建适配器以在 COM 级别提供完全控制。

第一个意味着更改界面,这会破坏您的要求。最后一个选项提供了最大的控制权,但明显更复杂(除非您已经了解 C++ COM 开发)。我会选择 VB.NET。

Named parametrised properties cannot be created in C# (only a single default one, called this is available).

There are a number of options:

  • Change the interface (but that misses the point as client code will need to change).
  • Use VB (.net), which can create such properties.
  • Create an adapter in C++ to give complete control at a COM level.

The first would mean changing the interface, which breaks your requirement. The final option gives the most control but is significantly more complex (unless you already know C++ COM development). I would go with VB.NET.

梦里的微风 2024-11-12 16:09:55

索引属性在 .Net/C# 中可用,但您无法命名它们:

public String this[long index]
{
    get; set;
}

这会生成一个名为 Item 的属性,但您不会在 C# 中使用此名称:

myObj[1L];

如果您想调用使用 C# 4.0 在 COM 中使用命名属性,您可以:

excel.Range["a"];

http://blogs.msdn.com/b/kirillosenkov/archive/2009/10/20/indexed-properties-in-c-4-0.aspx

最后,如果如果您想要以 COM 语言命名索引,可以使用 IndexerNameAttribute 将索引器公开为命名属性。

Indexed properties are available in .Net/C#, but you can't name them:

public String this[long index]
{
    get; set;
}

This makes a property called Item but you don't use this name in C#:

myObj[1L];

If you want to call out to a named property in COM with C# 4.0, you can:

excel.Range["a"];

http://blogs.msdn.com/b/kirillosenkov/archive/2009/10/20/indexed-properties-in-c-4-0.aspx

Finally, if you want to have the index named for COM languages, you can use the IndexerNameAttribute to expose the indexer as a named property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文