Search code examples
c#com

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


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.


Solution

  • 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.