Delegaty ogólne (C++/CLI)

Możesz użyć ogólnych parametrów typu z delegatami. Aby uzyskać więcej informacji na temat delegatów, zobacz delegate (C++/CLI i C++/CX).

Składnia

[attributes]
generic < [class | typename] type-parameter-identifiers>
[type-parameter-constraints-clauses]
[accessibility-modifiers] delegate result-type identifier
([formal-parameters]);

Parametry

Atrybuty
(Opcjonalnie) Dodatkowe informacje deklaratywne. Aby uzyskać więcej informacji na temat atrybutów i klas atrybutów, zobacz Atrybuty.

type-parameter-identifier(s)
Rozdzielana przecinkami lista identyfikatorów parametrów typu.

type-parameter-constraints-clauses
Przyjmuje formularz określony w ograniczeniach dotyczących parametrów typu ogólnego (C++/CLI)

modyfikatory ułatwień dostępu
(Opcjonalnie) Modyfikatory ułatwień dostępu (np. public, private).

typ wyniku
Zwracany typ delegata.

Identyfikator
Nazwa delegata.

parametry formalne
(Opcjonalnie) Lista parametrów delegata.

Przykłady

Parametry typu delegata są określane w punkcie, w którym jest tworzony obiekt delegata. Zarówno delegat, jak i metoda skojarzona z nim muszą mieć ten sam podpis. Poniżej przedstawiono przykład ogólnej deklaracji delegata.

// generics_generic_delegate1.cpp
// compile with: /clr /c
generic <class ItemType>
delegate ItemType GenDelegate(ItemType p1, ItemType% p2);

Poniższy przykład pokazuje, że

  • Nie można użyć tego samego obiektu delegata z różnymi typami skonstruowanymi. Utwórz różne obiekty delegatów dla różnych typów.

  • Delegat ogólny może być skojarzony z metodą ogólną.

  • Gdy metoda ogólna jest wywoływana bez określania argumentów typu, kompilator próbuje wywnioskować argumenty typu dla wywołania.

// generics_generic_delegate2.cpp
// compile with: /clr
generic <class ItemType>
delegate ItemType GenDelegate(ItemType p1, ItemType% p2);

generic <class ItemType>
ref struct MyGenClass {
   ItemType MyMethod(ItemType i, ItemType % j) {
      return ItemType();
   }
};

ref struct MyClass {
   generic <class ItemType>
   static ItemType MyStaticMethod(ItemType i, ItemType % j) {
      return ItemType();
   }
};

int main() {
   MyGenClass<int> ^ myObj1 = gcnew MyGenClass<int>();
   MyGenClass<double> ^ myObj2 = gcnew MyGenClass<double>();
   GenDelegate<int>^ myDelegate1 =
      gcnew GenDelegate<int>(myObj1, &MyGenClass<int>::MyMethod);

   GenDelegate<double>^ myDelegate2 =
      gcnew GenDelegate<double>(myObj2, &MyGenClass<double>::MyMethod);

   GenDelegate<int>^ myDelegate =
      gcnew GenDelegate<int>(&MyClass::MyStaticMethod<int>);
}

W poniższym przykładzie zadeklarowany jest ogólny delegat GenDelegate<ItemType>, a następnie tworzy wystąpienie go przez skojarzenie go z metodą MyMethod używającą parametru ItemTypetypu . Dwa wystąpienia delegata (liczba całkowita i podwójna) są tworzone i wywoływane.

// generics_generic_delegate.cpp
// compile with: /clr
using namespace System;

// declare generic delegate
generic <typename ItemType>
delegate ItemType GenDelegate (ItemType p1, ItemType% p2);

// Declare a generic class:
generic <typename ItemType>
ref class MyGenClass {
public:
   ItemType MyMethod(ItemType p1, ItemType% p2) {
      p2 = p1;
      return p1;
    }
};

int main() {
   int i = 0, j = 0;
   double m = 0.0, n = 0.0;

   MyGenClass<int>^ myObj1 = gcnew MyGenClass<int>();
   MyGenClass<double>^ myObj2 = gcnew MyGenClass<double>();

   // Instantiate a delegate using int.
   GenDelegate<int>^ MyDelegate1 =
      gcnew GenDelegate<int>(myObj1, &MyGenClass<int>::MyMethod);

   // Invoke the integer delegate using MyMethod.
   i = MyDelegate1(123, j);

   Console::WriteLine(
      "Invoking the integer delegate: i = {0}, j = {1}", i, j);

   // Instantiate a delegate using double.
   GenDelegate<double>^ MyDelegate2 =
      gcnew GenDelegate<double>(myObj2, &MyGenClass<double>::MyMethod);

   // Invoke the integer delegate using MyMethod.
   m = MyDelegate2(0.123, n);

   Console::WriteLine(
      "Invoking the double delegate: m = {0}, n = {1}", m, n);
}
Invoking the integer delegate: i = 123, j = 123
Invoking the double delegate: m = 0.123, n = 0.123

Zobacz też

Typy ogólne