Passing strongly typed arguments in .NET COM interop

前端 未结 2 1745
半阙折子戏
半阙折子戏 2021-01-05 14:03

I have two .NET classes exposed via COM interop - let\'s say Foo and Bar, and I need to pass an argument of type Foo to a method defined in Bar. Something like this:

2条回答
  •  鱼传尺愫
    2021-01-05 14:44

    Make sure, you define a GUID attribute, this is necessary if you make a QueryInterface (VB does probably). You have to generate a new unique GUID for every comvisible class.

    [Guid("77777777-3333-40df-9C0D-2B580E7E1F3B")]
    [ComVisible(true)]
    public class Foo
    {
    }
    

    Then i would strongly recommend to write interfaces for your COM objects, and set the ClassInterface to None, so no internals are revealed. Your typelibrary will be much cleaner this way.

    [Guid("88888888-ABCD-458c-AB4C-B14AF7283A6B")]
    [ComVisible(true)]
    public interface IFoo
    {
    }
    
    [ClassInterface(ClassInterfaceType.None)]
    [Guid("77777777-3333-40df-9C0D-2B580E7E1F3B")]
    [ComVisible(true)]
    public class Foo : IFoo
    {
    }
    

提交回复
热议问题