我有一个 .NET dll,它有一些暴露给 com 的接口(interface)\类。在构建过程中会生成一个 .tlb 文件,并且该 tlb 被一些 c++ 代码引用。因此,编译器会为 tlb 生成一个 .tlh 文件。
当我在本地运行构建时,其中一个接口(interface)中的属性之一最终会在 tlh 中具有不同名称的相应方法。 .net 代码中的属性称为 PropertyA,最终称为 get_propertyA,而 PropertyB 最终称为 get_PropertyB。发生这种情况时,我没有眨眼,只是使用了 tlh 中定义的方法并假设一切都是 hunky dory,但是当我提交这些更改时,构建对其他人不起作用,因为编译器生成了名为 get_PropertyA 的属性和get_PropertyB(注意 propertyA 中的大小写不匹配)。
两台机器上生成的 tlb 文件是相同的(根据十六进制比较器),并且 tlh 文件都是由相同的编译器版本生成的。
构建过程通过执行以下操作创建 tlb: regasm path\to\dll\Mydll.dll -tlb:path\to\output\mydll.tlb
任何想法为什么我的本地版本最终会出现名称不正确的属性?或者我能做些什么来解决它?
更新:我读到 tlbexp 将使用它找到的字符串的第一个版本,并且可以通过重新编译来改变。虽然我没有使用 tlbexp,但我想知道这是否是问题所在。我找到了与我的方法(在其他方法中)同名的参数,但开头有一个小写字母。所以我把这些都换了。重建,没有变化。所以我然后重命名了我的 COM 方法。重新构建并得到预期的缺失方法错误。将该方法重命名为原始名称,嘿,它似乎已修复。由于它现在似乎可以工作并且我不能让它再次失败,所以我无法尝试建议的解决方案,但我喜欢重命名的想法,以防将来发生这种情况。
最佳答案
我有同样的问题。
通过另一个 SO 问题 (https://stackoverflow.com/questions/708721/compare-type-libraries-generated-by-tlbexp),我发现了以下社区内容:
http://social.msdn.microsoft.com/Forums/en-US/clr/thread/5003c486-ed3f-4ec8-8398-a1251b0f9e74
引用该内容:
In the documentation of tlbexp, there is one useful community content:
http://msdn2.microsoft.com/en-gb/library/hfzzah2c(VS.80).aspx
Quote:
"The reason for the /names option is that type libraries store each identifier in a case-insensitive table. The first case encountered wins. So a class called Monitor might end up being exposed as "monitor" if there's a parameter with such a name encountered first. (And the order in which identifiers are encountered can vary simply by recompiling your assembly!) /names can guarantee stable casing."
The root cause seems to be a bug in the midl, described here:
http://support.microsoft.com/default.aspx?scid=kb;en-us;220137
Quote:
"When there are two identifiers that differ only by case, the case of the second identifier is changed to reflect the case of the first."
So as a solution, I unchecked the option "register for COM interop" in the project settings and added the post-build-steps
"$(DevEnvDir)....\SDK\v2.0\Bin\tlbexp" $(TargetFileName) /names:"$(ProjectDir)Names.txt" %windir%\Microsoft.NET\Framework\v2.0.50727\regasm $(TargetFileName)
The names file contains the entriesthat define how capizalization should be done. In my case it contains only one line:
ID
Best Regards
Bernd Ritter
使用/names 为我解决了这个问题。
关于.net - 在 2 台机器上生成的 .tlh 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/911461/