0

I'm trying to use a old VB library in a C# application.

This library has a couple of classes, one in particular has a method returns an instance of itself example: Posts post = new Posts().GetLast();

The Posts class has a property Comments that is actually of type object. In VB I can iterate trough the Comments property like trough a Microsoft.VisualBasic.Collection, but in C# I cannot cast the property, in order to use it, to anything. I tried importing the Microsoft.VisualBasic namespace, and typecasting to Collection, but it didn't work returning a error that said System.__ComObject has to be cast to a spcific Interface from the library.

Inside the collection there are objects that implement the interface IComments, I cannot cast to the interface because it is a collection of the interface.

I also tried casting to ICollection (the interface that is implemented by Microsoft.VisualBasic.Collection) but with no positive result.

Any idea on how to solve this issue, my only way out of this problem is by changing the C# application to a VB one (not that difficult, but still a thing that i would prefer avoiding)

Atzoya
  • 1,377
  • 13
  • 31
  • did you go through answer to this question? --> http://stackoverflow.com/questions/2151861/casting-a-system-comobject-in-c-sharp-via-reflection – rt2800 Apr 25 '12 at 08:42
  • I did see this specific answer, and if i understand correctly, this means that a typecast is not possible if I do not have control of the class library (I cannot implement any interface on the object)? – Atzoya Apr 25 '12 at 08:52

1 Answers1

0

By trying all the Interfaces I can think of , using the as operator and comparing the result null I found out that the the property can be cast as IEnumerable (logicaly Microsoft.VisualBasic.Collection implements ICollection and ICollection implements IEnumerable).

Atzoya
  • 1,377
  • 13
  • 31