Problems with naming(lowercase/uppercase) when exporting COM component from C#

扶醉桌前 提交于 2020-01-02 19:12:29

问题


I have a C# COM DLL that defines some interfaces/types that I use from a C++ ATL/COM server. From time to time, be it on my machine, or randomly on other machines where I build the same projects (the ATL *.exe and the C# DLL) I get different compile errors related to exported C# struct members that are part of the COM interface. Here is an example:

public enum TemporaryPolicyType
{
    UntilTime = 0,
    ForNextMinutes
}

[Guid("6F8CD968-DA76-44CA-B4E1-C495AB5003BD")]
public struct TemporaryPolicyData
{
    public TemporaryPolicyType Type;
    public DateTime Timestamp;
    public DateTime EndTime;
    public int EchartMinutes;
}

For example in this particular case, C# will "export" sometimes, on some machines, member "Type" with lowercase letters, and on some other machines using it like it is in code will work just fine.

I am using VS 2010.


回答1:


This is a known problem with type libraries. It is in fact by design for very obscure reasons, almost certainly having something to do with not knowing whether the compiler that reads the type library is sensitive to casing. A Basic compiler is not, a C++ compiler is. The fix was crude however, it does not classify the identifier by usage.

If any declaration before your structure declaration also contains an identifier named "type" then the rest of the identifiers will use the letter casing of that first one. The most unexpected case is when it is the name of a method parameter, most typically written in lower-case. Bummer if "Type" is the name of, say, a property, it will be renamed to "type". The crude part.

The only workaround is to use a more specific name that doesn't clash.



来源:https://stackoverflow.com/questions/7261517/problems-with-naminglowercase-uppercase-when-exporting-com-component-from-c-sh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!