Listing 6
using namespace System;
delegate void D(int x);
ref class Test
{
String^ objName;
public:
Test(String^ objName)
{
this->objName = objName;
}
void M(int i)
{
Console::WriteLine("Object {0}: {1}", objName, i);
}
};
void ProcessList(D^ del, int value, Object^ objToExclude);
int main()
{
/*1*/ Test^ t1 = gcnew Test("t1");
D^ cd1 = gcnew D(t1, &Test::M);
/*2*/ Test^ t2 = gcnew Test("t2");
D^ cd2 = gcnew D(t2, &Test::M);
/*3*/ Test^ t3 = gcnew Test("t3");
D^ cd3 = gcnew D(t3, &Test::M);
/*4*/ D^ list = cd1 + cd2 + cd3 + cd2;
/*5a*/ ProcessList(list, 100, nullptr);
/*5b*/ ProcessList(list, 200, t1);
/*5c*/ ProcessList(list, 300, t2);
/*6a*/ D^ cd4 = cd1 + cd2;
/*6b*/ D^ cd5 = (D^)cd4->Clone();
/*6c*/ ProcessList(cd4, 5, nullptr);
/*6d*/ ProcessList(cd5, 6, nullptr);
}
void ProcessList(D^ del, int value, Object^ objToExclude)
{
/*7*/ if (del == nullptr)
{
return;
}
/*8*/ else if (objToExclude == nullptr)
{
del(value);
}
else
{
/*9*/ array<Delegate^>^ delegateList = del->GetInvocationList();
for each (Delegate^ d in delegateList)
{
/*10*/ if (d->Target != objToExclude)
{
/*11*/ ((D^)d)(value);
}
}
}
}