简体   繁体   English

COM的put_XXX方法在.NET RCW中是否更改为set_XXX

[英]Does COM's put_XXX methods change to set_XXX in a .NET RCW

I have a COM component that has get_XXX and put_XXX methods inside it. 我有一个COM组件,其中包含get_XXX和put_XXX方法。 I used it in a .NET project and a RCW was generated for it. 我在.NET项目中使用了它,并为此生成了RCW。 I now see get_XXX and set_XXX methods and NOT the put_XXX one? 我现在看到的是get_XXX和set_XXX方法,而不是put_XXX方法? Is that automatic or defined somewhere in IDL? 这是自动的还是在IDL中定义的?

These are property accessor methods. 这些是属性访问器方法。 A compiler that uses the COM server is expected to generate a call to get_Xxx() when the client program reads the property, put_Xxx() when it writes it. 使用COM服务器的编译器应在客户端程序读取属性时生成对get_Xxx()的调用,而在写入属性时生成对put_Xxx()的调用。 A special one that C# doesn't have at all is putref_Xxx(), used to unambiguously access an object instead of a value. C#根本没有的特殊对象是putref_Xxx(),用于明确访问对象而不是值。

The normal translation performed by Tlbimp.exe is as a plain C# property. Tlbimp.exe执行的常规转换是普通的C#属性。 But that doesn't always work, C# is a lot more strict about what a property can look like: 但这并不总是有效,C#对属性的外观提出了更为严格的要求:

  • The default property, the one that's annotated as DISPID_VALUE (dispid 0) must take a single argument to be compatible. 默认属性(注释为DISPID_VALUE(dispid 0)的属性) 必须使用单个参数才能兼容。 This maps to the C# indexer property , the one that makes it look like you are indexing an array. 这映射到C#indexer属性 ,该属性使它看起来像是在索引数组。
  • Any other property cannot take an argument, C# does not supported indexed properties other than the indexer. 其他任何属性都不能带有参数,C#除了索引器之外不支持索引属性。
  • C# does not have the equivalent of putref_Xxx(), the syntax ambiguity cannot occur in a C# program because of the previous two bullets. C#不具有putref_Xxx()的等效项,由于前两个项目符号,因此在C#程序中不会出现语法歧义。 And the core reason that the C# team decided to put these restrictions in place, they greatly disliked ambiguity in the language. C#团队决定实施这些限制的核心原因是,他们极大地讨厌语言的歧义。

So Tlbimp.exe is forced to deal with these restrictions, if the COM property accessors are not compatible then it must fall back to exposing them as plain methods instead of a property. 因此,Tlbimp.exe被迫处理这些限制,如果COM属性访问器不兼容,则它必须退回到将它们作为普通方法而不是属性公开。 With default names, they'll get the get_ and set_ prefixes. 使用默认名称,它们将获得get_set_前缀。 The latter one explains your question, they didn't pick put_ for an otherwise unclear reason. 后者解释了您的问题,出于其他不清楚的原因,他们没有选择put_

Notable is that C# version 4 relaxed several of these restrictions, most of all to make interop with Office programs easier. 值得注意的是,C#版本4放宽了这些限制中的几个限制,最重要的是使与Office程序的互操作更加容易。 Which was quite painful in earlier C# versions, to put it mildly. 温和地说,这在早期的C#版本中是很痛苦的。 It extended the property syntax to lessen the pain, but only for COM interop. 它扩展了属性语法以减轻痛苦,但仅适用于COM互操作。 Very strongly recommended if you are still stuck on an old version of .NET, now is a good time to consider updating. 强烈建议您仍然使用旧版本的.NET,现在是考虑进行更新的好时机。

The properties themselves have to prefixes ( put_ etc.), they have names, getter method, setter method, but no prefixes. 属性本身必须带有前缀( put_等),它们具有名称,getter方法,setter方法,但没有前缀。 Method table generated from type library receives prefixes to distinguish between getters and setters, hence the prefixes. 从类型库生成的方法表接收前缀以区分getter和setter,因此是前缀。 Prefix string exactly depends on preference of the one who generates the names. 前缀字符串完全取决于生成名称的人的偏好。

See also: 也可以看看:

By default, low-level propget, propput, and propputref methods are exposed by member functions named with prefixes of get_, put_, and putref_ respectively. 默认情况下,低级propget,propput和propputref方法由分别以get_,put_和putref_前缀命名的成员函数公开。 These prefixes are compatible with the names used in the header files generated by MIDL. 这些前缀与MIDL生成的头文件中使用的名称兼容。

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

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