let_property方法的好奇心

发布于 2024-08-13 12:05:33 字数 1204 浏览 4 评论 0原文

每个 .net 开发人员都知道属性的概念。大约 99.99%,它只是将两个方法(一个 getter 和一个 setter)粘合在一起的元数据。

对于事件及其添加、删除和调用方法通常也是如此。

ECMA-335 描述了一种“其他”方法语义,适用于属性或事件。从概念上讲,一个属性或一个事件可以有多个“其他”方法。

今天是我用“其他”方法偶然发现一处房产的第一天。当然,它必须与 COM 有关。 EnvDTE 程序集中的接口 EnvDTE.Property(用于将插件写入 Visual Studio)包含一个定义如下的属性:

.property object Value()
{
  .custom instance void [mscorlib]System.Runtime.InteropServices.DispIdAttribute::.ctor(int32) = ( 01 00 00 00 00 00 00 00 ) 
  .get instance object EnvDTE.Property::get_Value()
  .other instance void EnvDTE.Property::let_Value(object)
  .set instance void EnvDTE.Property::set_Value(object)
}

let_Value 定义为:

.method public hidebysig newslot specialname abstract virtual 
        instance void  let_Value([in] object  marshal( struct) lppvReturn) runtime managed internalcall
{
  .custom instance void [mscorlib]System.Runtime.InteropServices.DispIdAttribute::.ctor(int32) = ( 01 00 00 00 00 00 00 00 ) 
}

显然,VBScript 和 VB.NET 之前的 VB 版本可以使用 Let 定义属性关键词。 Let 与 Set 具有相同的签名。我感觉这里有某种关系。

但有人知道这个属性是如何用 EnvDTE 编写的语言声明的吗?我如何创建具有相同图案的组件(不使用 ilasm,那太简单了)?有人遇到过类似的财产吗?

是否有人见过其他“其他”属性,其语义可能与此不同?如果是的话,他们习惯什么?

Every .net developer knows about the concept of properties. A rough 99.99%, it's just a piece of metadata gluing together two methods, a getter, and a setter.

Same thing usually goes for events, with their add, remove, and invoke method.

The ECMA-335 describes a «Other» kind of method semantic, that would apply to either a property or an event. Conceptually, a property or an event could have a multiple number of «other» methods.

Today's the first day I stumbled upon a property with an «other» method. And of course it had to be related to COM. The interface EnvDTE.Property, in the EnvDTE assembly (used to write addins to Visual Studio), contains a property defined as follows:

.property object Value()
{
  .custom instance void [mscorlib]System.Runtime.InteropServices.DispIdAttribute::.ctor(int32) = ( 01 00 00 00 00 00 00 00 ) 
  .get instance object EnvDTE.Property::get_Value()
  .other instance void EnvDTE.Property::let_Value(object)
  .set instance void EnvDTE.Property::set_Value(object)
}

With let_Value being defined as:

.method public hidebysig newslot specialname abstract virtual 
        instance void  let_Value([in] object  marshal( struct) lppvReturn) runtime managed internalcall
{
  .custom instance void [mscorlib]System.Runtime.InteropServices.DispIdAttribute::.ctor(int32) = ( 01 00 00 00 00 00 00 00 ) 
}

Apparently, VBScript and versions of VB before VB.NET can define properties using a Let keyword. And the Let has a same signature as the Set. I sense that there's a relationship here.

But does anyone know how this property have been declared in the language that EnvDTE has been written with? How could I create an assembly with the same pattern (without using ilasm, that would be too easy)? And does anyone have faced a similar property?

And does anyone have seen other «other» properties, with maybe a different semantic than this one? And if yes, what are they used to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城孤痞 2024-08-20 12:05:33

这是一个在 VB 中表现出来的 COM 东西。 设置分配一个引用来替换属性的引用项,而 Let 预期将操作数的内容复制到现有属性中。 (另请参阅属性获取)。

IIRC 这不是一个核心的 COM 东西,更多的是当语言没有足够的表达能力来处理价值与引用问题到足够精确的程度时使用的东西 - 我相信它可能只适用于当你使用 IDispatch 时(你'通过属性 id 而不是方法来寻址)而不是自定义接口(您始终必须解析方法并调用它)。我很确定 VB.NET(或其他 .NET 语言)不会出现这样的东西,因此它们是罕见的东西。

Essential COM by Box 没有提及它(仅用于 get 和 set 的 propget 和 propput)。 COM IDL 和Al Major 博士的界面设计在 P106 上提到了这一点,并说道:

dispinterface DMyInterface { 方法:
...
[id(3), propputref] void lMyProp([in] IDispatch *pDisp);
}

propputref 属性是一个奇怪的小东西,它起源于 Visual Basic 语法的特性。考虑以下因素:

Dim val 作为 DMyOtherInterface
Dim var 作为 DMyInterface

设置 var.lMyProp = val
var.lMyProp = val

这两个赋值都是允许的,但含义完全不同。在第一个分配中使用 Set 关键字表明 lMyProp 正在被分配一个接口 [...]。第二个赋值是一个简单的赋值,其中 val 对象的,它是 DMyOtherInterface 接口的默认成员的值 (默认成员是由 DISPID_VALUE ID 标记的成员(稍后将对此进行解释),被分配给 DMyInterfacelMyProp 属性界面。

第一个赋值是使用与 lMyProp 属性关联的 propputref 方法执行的,而第二个赋值则使用 propput 方法。为了使其工作,必须定义 propputref propput 方法。 如果您对这种做事方式感到困惑,那么您并不孤单。虽然 VB 拥有许多从根本上改变了编程本质的优秀特性,但该语言的定义主要是市场驱动的而不是设计的,有时它会表现出来

有趣的是,自从 2000 年初在 COM 和 .COM 崩溃之前阅读这本书以来,我就再也没有使用过这本书(不过就其目的而言,它是一本好书)。感谢您的回忆之旅 - 我喜欢人们告诉我们编程越来越难的方式!

我没有带 Lidin 书来看看它是否提到了 .other 但我确信你会这样做(顺便说一句,非常感谢 Mono.Cecil)

It's a COM thing which is surfaced in VB. Set assigns a reference to replace a property's referred-to item, whereas Let is expected to copy the content of the operand into the existing property. (See also Property Get).

IIRC this is not a core COM thing, more something that's used where a language doesnt have sufficient expressive power to deal with value vs reference issues to a sufficiently precise degree - I believe it may only apply when you're using IDispatch (where you''re addressing by property id rather than method) rather than a custom interface (where you always have to resolve to a method and call that). I'm pretty sure VB.NET (or other .NET languages) doesnt surface such things and hence they're a rare thing.

Essential COM by Box doesnt mention it (only propget and propput for get and set). COM IDL & Interface Design by Dr Al Major mentions it on P106 ans says:

dispinterface DMyInterface { methods:
...
[id(3), propputref] void lMyProp([in] IDispatch *pDisp);
}

The propputref attribute is an odd little thing that has its origins in the idiosynracies of Visual Basic syntax. Consider the following:

Dim val as DMyOtherInterface
Dim var as DMyInterface

Set var.lMyProp = val
var.lMyProp = val

The two assignments are both permissible but mean completely different things. The use of the Set keyword in the first assginment indicates that lMyProp is being assigned an interface [...]. The second assignment is a simpe one, where the value of the val object, which is the value of the default member of the DMyOtherInterface interface (the default member is the member tagged by the DISPID_VALUE ID, as will be explained shortly), is being assigned to the lMyProp property of the DMyInterface interface.

The first assignment is carried out using the propputref method associated with the lMyProp property, while the second assignment uses the propput method. In order for this to work, both the propputref and propput methods must be defined. If you're confused by this way of doing things, you're not alone. While VB has many good features that have fundamentally changed the nature of programming, the definition of the language was predominantly market-driven rather than being designed, and sometimes it shows.

Amusingly I havent ever used the Major book since reading it in early 2000 before the COM and .COM bust (Its a good book for its purpose though). Thanks for the trip down memory lane - I love the way people tell us that programming keeps getting harder!

Dont have the Lidin book with me to see if it mentions .other but I'm sure you do (BTW thanks a lot for Mono.Cecil)

断爱 2024-08-20 12:05:33

Visual Studio 自动化基于 COM。有问题的属性可能是通过支持 COM Interop 的工具生成的 (tlbimp 潜力)。我怀疑有人用实际的基于 .Net 的语言编写了这个代码。

Visual Studio automation is based upon COM. The property in question has probably been generated via a tool to support COM Interop (tlbimp potential). I doubt anyone coded this in an actual .Net based language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文