次の方法で共有


ジェネリックの使用 (C++/CLI)

.NET の 1 種類の言語で作成されたジェネリックは、他の .NET 言語で使用される場合があります。テンプレートとは異なり、コンパイルされたアセンブリのジェネリックは、一般に残ります。したがって、 1 が異なるアセンブリとジェネリック型が定義されているアセンブリは異なる言語のジェネリック型のインスタンスを作成する場合があります。

解説

詳細については、次のトピックを参照してください。

d38y03h1.collapse_all(ja-jp,VS.110).gifDescription

この例では、 C# で定義されているジェネリック クラスを示しています。

d38y03h1.collapse_all(ja-jp,VS.110).gifコード

// consuming_generics_from_other_NET_languages.cs
// compile with: /target:library
// a C# program
public class CircularList<ItemType> {
   class ListNode    {
      public ItemType m_item;
      public ListNode next;
      public ListNode(ItemType item) {
         m_item = item;
      }
   }

   ListNode first, last;

   public CircularList() {}

   public void Add(ItemType item) {
      ListNode newnode = new ListNode(item);
      if (first == null) {
         first = last = newnode;
         first.next = newnode;
         last.next = first;
      }
      else {
         newnode.next = first;
         first = newnode;
         last.next = first;
      } 
   }

   public void Remove(ItemType item) {
      ListNode iter = first;
      if (first.m_item.Equals( item )) {
         first = 
         last.next = first.next;
      }
      for ( ; iter != last ; iter = iter.next )
         if (iter.next.m_item.Equals( item )) {
              if (iter.next == last)
                  last = iter;
              iter.next = iter.next.next;
              return;
          }
   }

   public void PrintAll() {
      ListNode iter = first;
      do {
         System.Console.WriteLine( iter.m_item );
         iter = iter.next;
      } while (iter != last);
   }
}

d38y03h1.collapse_all(ja-jp,VS.110).gifDescription

この例では、 C# で記述されたアセンブリを実行します。

d38y03h1.collapse_all(ja-jp,VS.110).gifコード

// consuming_generics_from_other_NET_languages_2.cpp
// compile with: /clr
#using <consuming_generics_from_other_NET_languages.dll>
using namespace System;
class NativeClass {};
ref class MgdClass {};

int main() {
   CircularList<int>^ circ1 = gcnew CircularList<int>();
   CircularList<MgdClass^>^ circ2 = gcnew CircularList<MgdClass^>();

   for (int i = 0 ; i < 100 ; i += 10)
      circ1->Add(i);
   circ1->Remove(50);
   circ1->PrintAll();
}

d38y03h1.collapse_all(ja-jp,VS.110).gif出力

90
80
70
60
40
30
20
10

参照

その他の技術情報

ジェネリック (C++ コンポーネント拡張)