let_property方法的好奇心
每个 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个在 VB 中表现出来的 COM 东西。 设置分配一个引用来替换属性的引用项,而 Let 预期将操作数的内容复制到现有属性中。 (另请参阅属性获取)。
IIRC 这不是一个核心的 COM 东西,更多的是当语言没有足够的表达能力来处理价值与引用问题到足够精确的程度时使用的东西 - 我相信它可能只适用于当你使用 IDispatch 时(你'通过属性 id 而不是方法来寻址)而不是自定义接口(您始终必须解析方法并调用它)。我很确定 VB.NET(或其他 .NET 语言)不会出现这样的东西,因此它们是罕见的东西。
Essential COM by Box 没有提及它(仅用于 get 和 set 的 propget 和 propput)。 COM IDL 和Al Major 博士的界面设计在 P106 上提到了这一点,并说道:
有趣的是,自从 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:
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)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.