COM/.NET接口名称中的非字母数字字符

Cia*_*ing 8 .net com typelib typename

我正在考虑使用#@字符!在我们的系统生成的一些COM接口中.COM类型库也导出到.NET.这些角色会在以后给我带来麻烦吗?

我今天大部分时间都测试了它,一切看起来都很好.我们的系统继续像往常一样工作.

我谨慎的原因是这些字符在MIDL中是非法的,它使用C语法作为类型名称.但我们不使用MIDL - 我们使用ICreateTypeInfo和ICreateTypeLib构建我们的类型库.看起来这只是一个MIDL限制,COM和.NET对非字母数字字符很满意.但也许有一些我不知道的东西......

Cia*_*ing 2

这就是我发现的。

\n\n

我认为这些名称在 COM 的二进制级别上毫无疑问是合法的,因为 COM 接口\xe2\x80\x99s 的名称是它的 IID,而文本名称只是文档。

\n\n

在.NET方面,相关规范是公共语言基础设施规范(ECMA-335,http://www.ecma-international.org/publications/standards/Ecma-335.htm。)我想知道.NET还是Mono在 \xe2\x80\x93 上添加自己的限制,这样做会降低互操作性,但这就是现实世界。

\n\n

第 8.5.1 节介绍了通用类型系统中的有效类型名称,并简单说明名称是使用代码点进行比较的。奇怪的是,它没有提及名称的组成,只提及名称的比较方式。本节由 MSDN 在http://msdn.microsoft.com/en-us/library/exy17tbw%28v=VS.85%29.aspx进行解释,其中表示唯一的两个限制是 (1) 类型名称是“编码为 Unicode(16 位)字符的字符串”,并且 (2) 它们不能包含嵌入的 0x0000。

\n\n

我引用了有关 16 位 Unicode 的内容,而不是解释它,因为它使用了不精确的语言。想必该页面的作者指的是 UTF-16。无论如何,ECMA-335 指定了逐字节比较,并且没有提及 Unicode(关于类型名称),也没有禁止嵌入零。也许 .NET 已经偏离了 CTS,尽管我对此表示怀疑。更有可能的是,该 MSDN 页面的作者在编写该页面时正在考虑编程语言。

\n\n

The Common Language Specification (also defined in ECMA-335) defines the rules for identifiers in source code. Identifiers aren\'t directly relevant to my question because my internal type names never appear in source code, but I looked into it anway. The CLS is a subset of the CTS, and as such its restrictions aren\xe2\x80\x99t necessarily part of the broader CTS. CLS Rule 4 says that identifiers must follow the rules of Annex 7 of Technical Report 15 of the Unicode Standard 3.0 - see http://www.unicode.org/reports/tr15/tr15-18.html. That document too is a little vague, in that it refers to "other letter" and "connector punctuations" but doesn\'t define them. This helped: http://notes.jschutz.net/topics/unicode/.

\n\n

Section 8.5.1 of the ECMA spec includes a non-normative note that a CLS consumer (such as C# or the Visual Studio type browser, I suppose) \xe2\x80\x9cneed not consume types that violate CLS Rule 4.\xe2\x80\x9d My proposed interface names do violate this Rule 4. This note seems to imply that a valid type may have a name that violates rule 4, and that a CLS consumer should either accept the rogue name or safely ignore it. (The Visual Studio type browser displays it without complaint.)

\n\n

So my proposed type names are generally illegal in source code. But note that section 10.1 (about identifiers in the CLS) says \xe2\x80\x9cSince its rules apply only to items exported to other languages, private members or types that aren\xe2\x80\x99t exported from an assembly can use any names they choose.\xe2\x80\x9d

\n\n

I conclude that it\'s safe to use the characters #@! in my type names as long as they remain in the binary domain and never need appear in source code nor outside the assembly. And in fact they\'re never used outside the COM server.

\n\n

A word about future-proofing... The CTS pretty much has nothing to say about the composition of type names, despite having a section called \xe2\x80\x9cValid names\xe2\x80\x9d (section 8.5.1). They might change that in the future, but this broad and liberal specification has invited us all to do what we like. If the CTS designers had wanted to leave room for change then surely they would have built in some provision for that, or at least been less generous.

\n