检索系统时的异常行为.__ comobject [英] Unusual behavior when retrieving a system.__comobject

查看:70
本文介绍了检索系统时的异常行为.__ comobject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用第三方dll,它返回一个System .__ ComObject。因此,在通常情况下,我通常使用foreach语句,它可以遍历此对象,以便我获取所需的数据。



例如

I am currently working with a 3rd party dll, which return a System.__ComObject. So under a usual circumstance I usually use a foreach statement that can iterate through this object in order for me to get the data I need.

For example

CWMetaData metadata = app.Clients.GetMetaData(userPath);
foreach(var data in metadata)
{
   var getvalue = data;
}





当我遍历该对象时,我得到一个System .__ ComObject。我认为这很奇怪,因为这个System .__ ComObject与for语句有不同的行为。



例如:



I am getting a System.__ComObject as I iterate through that object. Which I think is weird because this System.__ComObject has a different behavior with a for statement.

For example:

for(int x =0; x< metadata.Count;x++)
{
   var getvalue = metadata[x].Name;
}





在这个for语句中,我能够实际深入了解System .__ ComObject with甚至为System .__ ComObject提供智能感知。我不确定区别是什么,但我确信这两个示例都在迭代同一个对象。



我的问题是,为什么在示例一和示例二之间存在不同的行为。如果两个循环都是这样的话。



我尝试过:



我已阅读有关Marshaling的文档,但我认为它不适用于此案例



In this for statement I am able to actually to drill down into the System.__ComObject with which even provides intellisense for the System.__ComObject. I am not sure what the difference is, but I am sure that both example iterating through the same object.

My question is, why is why is there a different behavior between example one and example two. If both loops are doing the same.

What I have tried:

I have read documentation on Marshaling, but I think it does not apply to this case

推荐答案

为了更好地理解,您需要看看COM的内部工作原理。允许在.NET中进行枚举的COM对象(foreach)实际上返回一个IEnumVariant实现,因此返回的每个值都是一个自然的VARIANT,当编组到.NET中时,它变成一个类型对象。此对象的类型为VT_UNKNOWN或VT_DISPATCH允许.NET将其强制转换为ComObject,但此对象指向的实际COM对象未知。另一方面,Item属性通常会返回指向特定COM接口实现的指针,因此可以在编译时确定类型。
To understand better, you need to look at the inner workings of COM. A COM object that allows enumeration in .NET (foreach) actually returns an IEnumVariant implementation, so each value returned is a natural VARIANT, which when marshalled into .NET, becomes a type object. The fact that this object is of type VT_UNKNOWN or VT_DISPATCH allows .NET to cast it to a ComObject, but the actual COM object being pointed to by this object is not known. The Item property, on the other hand, will generally return a pointer to a specific COM interface implementation, and so the type can be determined at compile time.


如果查看定义 foreach(在C#参考中) [ ^ ]:
The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface. The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.

如果没有关于被调用对象的任何更多信息,并根据上述信息,我会得出结论 System.Collections.IEnumerable System.Collections.Generic.IEnumerable< T> 接口未实现C#。

Without any more information on the object being called and based on the information above, I would conclude that the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface is not implemented for C#.


这篇关于检索系统时的异常行为.__ comobject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆