Why should I not use AutoDual?

后端 未结 2 1734
我寻月下人不归
我寻月下人不归 2020-12-16 02:51

Up to now, I\'ve always decorated my .NET classes that I want to use from VB6 with the [AutoDual] attribute. The point was to gain Intellisense on .NET objects

2条回答
  •  独厮守ぢ
    2020-12-16 03:20

    I found a reliable way to both provide Intellisense for .NET objects in VB6, while at the same time not breaking the interface. The key is to mark each public method/property in the interface with DispatchID. Then the class must inherit from this interface - in the manner below.

    [Guid("BE5E0B60-F855-478E-9BE2-AA9FD945F177")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ICriteria
    {
        [DispId(1)]
        int ID { get; set; }
        [DispId(2)]
        string RateCardName { get; set; }
        [DispId(3)]
        string ElectionType { get; set; }
    }
    
    
    [Guid("3023F3F0-204C-411F-86CB-E6730B5F186B")]    
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("MyNameSpace.Criteria")]
    public class Criteria : ICriteria
    {
        public int ID { get; set; }
        public string RateCardName { get; set; }
        public string ElectionType { get; set; }
    }
    

    What the dispatch ID gives you is the ability to move around items in the class, plus you can now add new things to the class and not break the binary compatibility.

提交回复
热议问题