0

My COM interface:

[ComVisible(true)]
public interface IPersonAccessor
{
    Person GetById(int id);
}

The Person Class is a simple data class as below:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Do I need to do anything else for the Person class(putting attributes on person class etc) so that C++ code can access its properties?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97
  • Yes, it also has to be [ComVisible(true)]. Writing an interface for it is good style and the COM way, return IPerson instead. If you don't then you have to also apply [ClassInterface(ClassInterfaceType.AutoDual)] to the class so the C++ code can use early binding. Helps a lot to leverage the #import directive. You'll need it, writing COM interop code in C++ is pretty cruel. – Hans Passant Dec 07 '16 at 12:06
  • @HansPassant thanks for your reply. One additional thing, to return a collection from a COMVisible method, I can use the ICollection type. Can I use an ICollection of IPerson as a parameter of a COMVisible method? – Victor Mukherjee Dec 07 '16 at 12:54
  • @VictorMukherjee ICollection doesn't seem to be ComVisible, so I don't think you can. Either way, the more common way is to just use an array, which the C++ code will see as a SAFEARRAY. – user1610015 Dec 07 '16 at 14:13

0 Answers0