I want to ask about the IEnumerable interface, which as I understand it is used when working with collections (I just started to study them), but the topic is not about collections, but about the contents of the above interface:

 namespace System.Collections { // Сводка: // Предоставляет перечислитель, который поддерживает простой перебор элементов // неуниверсальной коллекции. [ComVisible(true)] [Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")] public interface IEnumerable { // Сводка: // Возвращает перечислитель, осуществляющий итерацию в коллекции. // // Возвращает: // Объект System.Collections.IEnumerator, который может использоваться для перебора // коллекции. [DispId(-4)] IEnumerator GetEnumerator(); } } 

So, in order, what I do not understand: string

 [ComVisible(true)] 

as the definition of Visual Studio gives when I move the cursor to a setting: ** class System.Runtime.InteropServices.ComVisibleAttribute

 InteropServices - ? COM - это что вообще ? ComVisibleAttribute - ? что за ComVisible [ComVisible(true)]- какую роль он выполняет 

Managed type \ member?

Further:

 [DispId(-4)] 

Again, the Visual Studio definition ** class System.Runtime.InteropServices.DispldAttribute Specifies the COM dispatch identifier (DISPID) for a method, field, or property. **

COM dispatch identifier (DISPID) - what!?

COM dispatching -?

DISPID -?

And then I go into this

 [DispId(-4)] 

And there it is:

 public sealed class DispIdAttribute : Attribute { // Сводка: // Инициализирует новый экземпляр класса DispIdAttribute, используя указанный // идентификатор DISPID. // // Параметры: // dispId: // Идентификатор DISPID для этого члена. public DispIdAttribute(int dispId); // Сводка: // Возвращает идентификатор DISPID для члена. // // Возвращает: // Идентификатор DISPID для этого члена. public int Value { get; } } 

and as I understood this

 [DispId(-4)] 

is the constructor of the class DispIdAttribute?

I beg you to give a detailed and detailed answer, without this: “there are some mechanisms there, but since you just started learning collections, you don’t need to and even interfere” - believe me, I need everything. You can answer the questions, even need them so that these disparate questions will eventually form one comprehensive answer.

  • 2
    How about google about COM technology in Windows? And the same Richter read ("Windows via C / C ++"). - free_ze
  • 2
    it is possible even in msdn immediately: ComVisibleAttribute and DispIdAttribute - Grundy
  • one
    @free_ze, in general, the IEnumerable question has little relevance - Grundy
  • @Grundy I agree, I did not contradict this. - free_ze

2 answers 2

For starters, there is such a thing - COM (read about it, there are many books). This is the standard for the interaction of modules in different languages. C # assemblies also support this standard at will (you can include it in assembly properties), so .NET objects can be used as COM objects, and thus with various native applications, as well as scripting languages ​​(for example, javascript).

Yes, because [ComVisible(true)] means that the interface will be visible to the COM subsystem itself, which means that if your method returns an IEnumerable<T> , then its result can be used with COM.

The basic interfaces in COM are IUnknown and IDispatch . The last one is especially important for scripts, since it allows you to call a method by a string name. DispID is a special attribute so that .NET can do automatic implementation of IDispatch .


Yes, and if your program does not export COM objects, and does not work with scripts, then most likely you don’t care, and you can safely ignore these attributes. If your program is completely in .NET, COM attributes are not important to you.

    Give a few partial answer

    [ComVisible(true)] - Everything in these brackets in front of classes, structures, methods and attributes are optional attributes (Custom Attributes). This is written in C #.

    Simplistically, we can say that an instance of the attribute class is created when the code is executed. Those. in your case, the ComVisible(true); constructor is ComVisible(true);

    Note that the class itself will be called ComVisibleAttribute - i.e. with the addition of the word Attribute. This is required by C #.

    COM technology is such a means of interaction between programs and libraries written in many different languages. The technology is great in itself. Books are separate. These attributes of the ComVisibleAttribute class control the interaction behavior between two programs using COM technology. Generally dll in Windows are written using COM.

    InteropServices is just a namespace which contains the ComVisibleAttribute class. You use this when writing a namespace.

    [DispId(-4)] will call the constructor of the class public sealed class DispIdAttribute : Attribute with parameter -4. The implementation of the class is hidden for you, so what is there to look more likely will not work.

    [Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")] - This specifies a unique identifier. It is always unique at all. (Theoretically and practically). It serves to uniquely identify any objects.

    You do not need these attributes. Risk to hang for a very long time. Som changed many times. For now, let them pass by.

    • "Generally dll in Windows is written using COM." - yah? :) - Pavel Mayorov
    • one
      It is generally unique only in practice. But not in theory. Just a chance of collision in practice is astronomically small. - D-side
    • @Pavel Mayorov Imagine! ActiveX components implement COM! Great technology, various uses. And of course you can create dll and without COM. Perhaps it was wrong not to write a word. Some dlls in Windows use COM - Ilya Labacheuski
    • one
      @PavelMayorov significant part of the dll - a slightly softer definition :) - PashaPash