COM Interop IDictionary - 如何在C#中检索值?

时间:2022-05-12 21:35:25

using: VS2008, C#

使用:VS2008,C#

I have a COM dll I need to use in a .NET project. In there I have a class with a method that returns an IDictionary object. IDictionary is defined in the COM dll so I'm not sure if it's the same as IDictionary in .NET.

我有一个COM dll我需要在.NET项目中使用。在那里,我有一个类,其方法返回一个IDictionary对象。 IDictionary是在COM dll中定义的,所以我不确定它是否与.NET中的IDictionary相同。

My problem: I know the dictionary keys and I want to retrieve the values. The documentation for this COM dll gives code in Classic ASP like

我的问题:我知道字典键,我想要检索值。此COM dll的文档提供了经典ASP中的代码

someValue = oMyDictionary.someDictionaryKey

My Question: How do I retrieve the values for the specific keys in C#?

我的问题:如何在C#中检索特定键的值?

When I create the IDictionary object in C# like this:

当我在C#中创建IDictionary对象时,如下所示:

IDictionary oDictConfig = oAppConfig.GetOptionsDictionary("");

VS2008 reckons this dictionary object (interface) has the following methods and properties:

VS2008估计这个字典对象(接口)有以下方法和属性:

Cast<>
Count
Equals
GetEnumerator
GetHashCode
GetMultiple
GetType
let_Value
OfType<>
Prefix
PutMultiple
ToString

Sorry if this is a silly question, but I can't see how to retrieve a value passing a key.

很抱歉,如果这是一个愚蠢的问题,但我看不到如何检索传递密钥的值。

3 个解决方案

#1


IDictionary has an Item property among its members that you access via [ ].

IDictionary在其成员中有一个Item属性,您可以通过[]访问它。

var obj = oDictConfig[key];

#2


Try this:

dicObject["myKey"]

#3


It looks like you are using the IDictionary COM interface from BizTalk Server 2000:

看起来您正在使用BizTalk Server 2000中的IDictionary COM接口:

http://msdn.microsoft.com/en-us/library/ms936717.aspx

This interface should also have a NewEnum property (check the interop assembly in Reflector) which should return an IEnumerable to you. This IEnumerable will be an enumeration of the keys, which you can then use with calls to the Value property (with the appropriate key of course) to get the values.

此接口还应具有NewEnum属性(检查Reflector中的interop程序集),该属性应返回给您的IEnumerable。此IEnumerable将是键的枚举,您可以使用它来调用Value属性(当然使用适当的键)来获取值。

#1


IDictionary has an Item property among its members that you access via [ ].

IDictionary在其成员中有一个Item属性,您可以通过[]访问它。

var obj = oDictConfig[key];

#2


Try this:

dicObject["myKey"]

#3


It looks like you are using the IDictionary COM interface from BizTalk Server 2000:

看起来您正在使用BizTalk Server 2000中的IDictionary COM接口:

http://msdn.microsoft.com/en-us/library/ms936717.aspx

This interface should also have a NewEnum property (check the interop assembly in Reflector) which should return an IEnumerable to you. This IEnumerable will be an enumeration of the keys, which you can then use with calls to the Value property (with the appropriate key of course) to get the values.

此接口还应具有NewEnum属性(检查Reflector中的interop程序集),该属性应返回给您的IEnumerable。此IEnumerable将是键的枚举,您可以使用它来调用Value属性(当然使用适当的键)来获取值。