简体   繁体   English

我为什么不使用AutoDual?

[英]Why should I not use AutoDual?

Up to now, I've always decorated my .NET classes that I want to use from VB6 with the [AutoDual] attribute. 到目前为止,我总是使用[AutoDual]属性来装饰我想要在VB6中使用的.NET类。 The point was to gain Intellisense on .NET objects in the VB6 environment. 关键是要在VB6环境中获得.NET对象的Intellisense。 However, the other day I googled AutoDual and the first answer is 'Do Not Use AutoDual'. 然而,前几天我用Google搜索AutoDual,第一个答案是“不要使用AutoDual”。

I've looked for coherent explanation of why I shouldn't use it, but could not find it. 我已经找到了为什么我不应该使用它,但找不到它的连贯解释。

Can someone here explain it? 有人可以解释一下吗?

I found a reliable way to both provide Intellisense for .NET objects in VB6, while at the same time not breaking the interface. 我找到了一种可靠的方法,既可以在VB6中为.NET对象提供Intellisense,又可以不破坏界面。 The key is to mark each public method/property in the interface with DispatchID. 关键是使用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. 调度ID为您提供的是能够移动类中的项目,此外您现在可以向类中添加新内容而不会破坏二进制兼容性。

I think this sums it up: 我想这总结了一下:

Types that use a dual interface allow clients to bind to a specific interface layout. 使用双接口的类型允许客户端绑定到特定的接口布局。 Any changes in a future version to the layout of the type or any base types will break COM clients that bind to the interface. 未来版本对类型布局或任何基类型的任何更改都将破坏绑定到该接口的COM客户端。 By default, if the ClassInterfaceAttribute attribute is not specified, a dispatch-only interface is used. 默认情况下,如果未指定ClassInterfaceAttribute属性,则使用仅调度接口。

http://msdn.microsoft.com/en-us/library/ms182205.aspx http://msdn.microsoft.com/en-us/library/ms182205.aspx

It increases the possibility that changing something in that class with the auto dual attribute will break someone else's code when the class is changed. 它增加了使用auto dual属性更改该类中某些内容的可能性,这会在更改类时破坏其他人的代码。 If gives the consumer the ability to do something that will quite possibly cause them issues in the future. 如果让消费者有能力做一些很可能在未来引起问题的事情。

The next option is ClassInterfaceType.AutoDual. 下一个选项是ClassInterfaceType.AutoDual。 This is the quick and dirty way to get early binding support as well (and make the methods show up in VB6 IntelliSense). 这也是获得早期绑定支持的快速而肮脏的方法(并使方法显示在VB6 IntelliSense中)。 But it's also easy to break compatibility, by changing the order of methods or adding new overloads. 但是通过改变方法的顺序或添加新的重载也很容易破坏兼容性。 Avoid using AutoDual. 避免使用AutoDual。

http://www.dotnetinterop.com/faq/?q=ClassInterface http://www.dotnetinterop.com/faq/?q=ClassInterface

I finally found the link that talks about what is going on with AutoDual and how it works: 我终于找到了关于AutoDual发生了什么以及它是如何工作的链接:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7fa723e4-f884-41dd-9405-1f68afc72597 http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7fa723e4-f884-41dd-9405-1f68afc72597

The warning against AutoDual isn't the fact that dual interfaces is bad but the fact that it auto-generates the COM interface for you. 对AutoDual的警告不是双接口坏的事实,而是它为您自动生成COM接口的事实。 That is bad. 那很不好。 Each time the COM interface has to be regenerated you'll get a new GUID and potentially new members. 每次必须重新生成COM接口时,您将获得新的GUID和可能的新成员。 If the GUID changes then you get a brand new interface/class as far as COM is concerned. 如果GUID发生变化,那么就COM而言,你会得到一个全新的接口/类。 For early binding you'd have to rebuild the clients each time the interface was regenerated. 对于早期绑定,每次重新生成接口时都必须重建客户端。 The preferred approach is to define the COM class interface explicitly with a GUID. 首选方法是使用GUID显式定义COM类接口。 Then all the early binding clients can use the defined interface and not worry about it changing on them during development. 然后,所有早期绑定客户端都可以使用已定义的接口,而不用担心它在开发过程中对它们进行更改。 That is why the recommended option is None to tell the CLR not to auto-generate it for you. 这就是推荐选项为None的原因,告诉CLR不要为你自动生成它。 You can still implement the dual interface though if you need it. 如果需要,您仍然可以实现双界面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM