Visual C++ .NET/Delegate/Delegate
Содержание
Adding and removing Delegates
#include "stdafx.h"
using namespace System;
delegate void OutputMessage(String ^name);
void SayHello(String ^name){
Console::Write("Hello there ");
Console::WriteLine(name);
}
ref class MyClass{
public:
static void Print1(String ^name);
void Print2(String ^name);
void Print3(String ^name);
};
void MyClass::Print1(System::String ^name){
Console::Write("1 ");
Console::WriteLine(name);
}
void MyClass::Print2(System::String ^name){
Console::Write("2 ");
Console::Write(name);
}
void MyClass::Print3(System::String ^name){
Console::Write("3 ");
Console::WriteLine(name);
}
void main(){
OutputMessage^ say;
say = gcnew OutputMessage(&SayHello);
say += gcnew OutputMessage(&MyClass::Print1);
MyClass ^computer = gcnew MyClass();
say = say + gcnew OutputMessage(computer, &MyClass::Print2);
say += gcnew OutputMessage(computer, &MyClass::Print3);
say->Invoke("s1");
say = say - gcnew OutputMessage(&MyClass::Print1);
say -= gcnew OutputMessage(computer, &MyClass::Print3);
say("s1");
}
Define delegate function
#include "stdafx.h"
using namespace System;
using namespace System::Threading;
ref class MyClass{
public:
property String^ Value;
MyClass(String^ s) { Value = s; }
};
delegate void DoSearch(String^, MyClass^);
ref class Database{
IAsyncResult^ result;
MyClass^ m_r;
public:
Database(String^ s) { m_r = gcnew MyClass(s); }
void Query(String^ queryString, MyClass^ r){
r->Value = "New Value";
}
void InitiateQuery(String^ queryString){
DoSearch^ qf = gcnew DoSearch(this, &Database::Query);
Console::WriteLine(m_r->Value);
result = qf->BeginInvoke(queryString, m_r,gcnew AsyncCallback(this, &Database::ProcessResult),qf);
}
bool IsQueryCompleted(){
return result->IsCompleted;
}
void ProcessResult(IAsyncResult^ result)
{
DoSearch^ caller = (DoSearch^) result->AsyncState;
caller->EndInvoke(result);
Console::WriteLine(m_r->Value);
}
};
int main(){
Database doc("Old Value");
doc.InitiateQuery("SELECT * FROM Employee ");
while (! doc.IsQueryCompleted() ){
Thread::Sleep(100);
}
}
delegate invocation list
#include "stdafx.h"
using namespace System;
delegate String^ MyDelegate();
ref class MyClass
{
public:
String^ f() { return "MyClass::f"; }
String^ g() { return "MyClass::g"; }
String^ h() { return "MyClass::h"; }
};
int main()
{
MyDelegate^ d;
MyClass^ r = gcnew MyClass();
d = gcnew MyDelegate(r, &MyClass::f);
d = (MyDelegate^) d->Combine(d, gcnew MyDelegate(r, &MyClass::g));
d = (MyDelegate^) d->Combine(d, gcnew MyDelegate(r, &MyClass::h));
String^ s = d->Invoke();
Console::WriteLine("Return value was {0}", s);
d = (MyDelegate^) d->Remove(d, gcnew MyDelegate(r, &MyClass::g));
s = d->Invoke();
Console::WriteLine("Return value was {0}", s);
for each (MyDelegate^ del in d->GetInvocationList())
{
s = del->Invoke();
Console::WriteLine("Return value was {0}", s);
}
}
delegate operators
#include "stdafx.h"
using namespace System;
delegate void MyDelegate();
ref class MyClass{
public:
void f() { Console::WriteLine("MyClass::f"); }
void g() { Console::WriteLine("MyClass::g"); }
};
int main()
{
MyDelegate^ d;
MyClass^ r = gcnew MyClass();
d += gcnew MyDelegate(r, &MyClass::f);
d += gcnew MyDelegate(r, &MyClass::g);
d->Invoke();
d -= gcnew MyDelegate(r, &MyClass::g);
d();
}