Click here to Skip to main content
15,887,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
CWMetaData metadata = app.Clients.GetMetaData(userPath);
foreach(var data in metadata)
{
   var getvalue = data;
}


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:
C#
for(int x =0; x< metadata.Count;x++)
{
   var getvalue = metadata[x].Name;
}


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
Posted
Updated 27-Feb-17 13:12pm
v3
Comments
F-ES Sitecore 27-Feb-17 9:53am    
What's your question?
erick manuel 27-Feb-17 10:03am    
My Question is, why is why is there a different behavior between example one and example two. If both loops are doing the same.
[no name] 27-Feb-17 10:05am    
No in fact they are not doing the same thing. In the foreach you are making a variable assignment. In the second you are accessing the properties of an object. 2 completely different things. Stop using var might make it more clear.
erick manuel 27-Feb-17 10:11am    
thank you
[no name] 27-Feb-17 9:58am    
I don't see why you would think this is "unusual behavior". Your code snippets are doing two different things.

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.
 
Share this answer
 
If you look at the definition for foreach (in C# Reference)[^]:
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.
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#.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900