揭露房产为Variant .NET中的互操作 [英] Exposing Property as Variant in .NET for Interop

查看:110
本文介绍了揭露房产为Variant .NET中的互操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET中创建一个包装类(VB.NET,因为它发生,但同样涉及到C#)暴露于COM,我试图总结的属性之一是一个变量。我以为我只想能够使用的对象,但我得到一个错误:

I am creating a wrapper class in .NET (VB.NET as it happens but is equally related to C#) that is exposed to COM and one of the properties I am trying to wrap is a Variant. I thought I would just be able to use an Object, but I get an error:

公共财产fieldValue方法([vFieldID为对象= -1])作为对象不能暴露于COM作为属性'让'。你将无法从Visual Basic 6.0使用的是让'语句非对象值(如数字或字符串)分配给该属性。*

Public Property FieldValue([vFieldID As Object = -1]) As Object cannot be exposed to COM as a property 'Let'. You will not be able to assign non-object values (such as numbers or strings) to this property from Visual Basic 6.0 using a 'Let' statement.*

我的财产申报是这样的:

My property declaration looks like this:

Public Property FieldValue(Optional ByVal vFieldID As Object = -1) As Object
    Get
        Return _objVAccess.FieldValue(vFieldID)
    End Get
    Set(ByVal value As Object)
        _objVAccess.FieldValue = value
    End Set
End Property

所以它的不是的一个COM中的条款对象我的属性返回从中可以得到整数数据库,字符串,日期等的值。有没有解决方法,这让物业让?

My property actually returns a value from the database which can be integer, string, date, etc so it isn't an object in terms of COM. Is there any workaround to this to allow property Let?

推荐答案

COM自动化支持默认属性,已DISPID 0,这在VB6 code用于有很大的影响的性质,产生真正的紧凑code。一个典型的例子是:

COM Automation supports a default property, the property that has dispid 0. This is used in VB6 code to great effect, generating really compact code. A typical example is:

rs!Customer = "foo"

这是语法糖:

rs.Fields.Item("Customer").Value = "foo"

三个默认的属性这里使用,而不在原来的语句被命名。该记录集接口字段属性作为默认属性,生产领域的接口引用。其中有项目属性作为生产现场接口引用的默认(索引)属性。它具有价值属性作为默认属性,产生一个变种。

Three default properties being used here without being named in the original statement. The Recordset interface has the Fields property as the default property, producing a Fields interface reference. Which has the Item property as the default (indexed) property producing a Field interface reference. Which has the Value property as the default property, producing a variant.

这是非常好的。然而,极端的语法糖这样的价格是烂牙。有一个语法歧义在一份声明中这样的:

Which is very nice. The price of extreme syntax sugar like this however is rotten teeth. There's a syntax ambiguity in a statement like:

Dim obj  
obj = someObject

这是怎么打算?你想分配someObject参考OBJ?还是你要分配someObject的的默认的属性的?非常不同的东西,的 OBJ 的类型将是完全不同的。这是解决了VB6与设置关键字。如果你要分配的对象引用,那么你必须写:

What is intended here? Do you want to assign the someObject reference to obj? Or do you want to assign the default property of someObject? Very different things, the obj type will be completely different. This was solved in VB6 with the Set keyword. If you want to assign the object reference then you have to write:

Set obj = someObject

和省略的设置的或使用的我们的明确的,如果你的意思是指定默认属性值。这是pretty令人讨厌,并一直在折磨着新手Visual Basic和VB脚本编程了很长的时间。

And you omit Set or use Let explicitly if you mean to assign the default property value. That's pretty yucky and has bedeviled newbie Visual Basic and VB script programmers for a very long time.

COM自动化通过允许财产拥有的两个的setter方法​​实现了这一点。分别为 propput propputref 的在那里IDL是propputref分配的对象之一。您还可以看到这回在IDispatch的定义,的IDispatch :: invoke()方法两个与DISPATCH_PROPERTYPUT和DISPATCH_PROPERTYPUTREF进行了区分。

COM Automation implements this by allowing a property to have two setters. Respectively propput and propputref in the IDL where propputref is the one that assigns an object. You can also see this back in the IDispatch definition, the IDispatch::Invoke() method distinguishes between the two with DISPATCH_PROPERTYPUT and DISPATCH_PROPERTYPUTREF.

邮编着VB.NET,微软决定歧义是太痛苦,消除了默认属性的概念。其中暗藏也退休了Set关键字。然而,这将产生一个新的问题,没有任何再方式来写一个[标记有ComVisible特性]类可以有类型对象的属性与接受对象引用的setter。语言的语法仅允许一个二传手,并在CLR的COM互操作层缺少管道合成两项。值得注意的是,这只是一个警告,你仍然得到propput二传手,你是不会得到propputref制定者。其中,据我可以告诉是一切仍要。

Zip forward to VB.NET, Microsoft decided that the ambiguity was too painful and eliminated the notion of a default property. Which blissfully also retired the Set keyword. This however produces a new problem, there isn't any way anymore to write a [ComVisible] class that can have a property of type Object with a setter that accepts an object reference. The language syntax permits only one setter and the COM interop layer in the CLR is missing the plumbing to synthesize two. Notable is that this is just a warning, you still get the propput setter, you just won't get the propputref setter. Which as far as I can tell is all you want anyway.

定义接口无论是在VB6哑类或明确写IDL和MIDL.EXE编译它的确周围航行警告的方式。作为这个问题。

Defining the interface either in a VB6 dummy class or by writing the IDL explicitly and compiling it with midl.exe is indeed a way to sail around the warning. As shown by John Rivard in this question.

这篇关于揭露房产为Variant .NET中的互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆