Why is System.IO.Path (a class with only static elements) COM visible?

Why is System.IO.Path COM displayed when it contains only static members and fields?

[ComVisibleAttribute(true)] public static class Path 

I got the impression that you cannot call static member functions of a COM object (and, in addition, the class will need a default constructor that Path does not have).

Also pay attention to SO sharptooth user comment :

It is also worth noting that it does not have a Guid attribute, which most likely means that the class identifier will be regenerated every time it compiles.

Why is Path COM visible and what can be done with it?

+5
source share
1 answer

Of course, this is a mistake. This is a good company, many other classes from mscorlib have the same problem. For example, Registry, Directory, File, Buffer, Environment, Nullable, Monitor, Timeout. But not sequentially, the attribute was correctly omitted for BitConverter, Console, Convert, GC, Math, etc.

The attribute is otherwise important for many classes in mscorlib; CLR user hosts and scripting languages ​​depend on it. It seems that the Microsoft programmer who used this attribute just worked on autopilot. The mistake is immaterial, TLBEXP knows how to deal with it. The component receives the [noncreatable] attribute, so client programs cannot instantiate the class. And auto-generated interfaces are empty. Thus, the type is simply not applicable, and you cannot use it either.

If you really want to use System.IO.Path from the COM client program, you need to write a shell class [ComVisible] for it. Non-static, of course, every method you write can simply delegate directly to one of the Path methods. You want the [appobject] attribute to be selected in the class, so it behaves statically in a client program that supports the attribute, unfortunately .NET does not have an attribute for it.

+5
source

All Articles