65 lines
No EOL
1.8 KiB
C++
65 lines
No EOL
1.8 KiB
C++
#include "stdafx.h"
|
|
#include "VariableCapture.h"
|
|
|
|
|
|
generic<typename T1>
|
|
public ref class VariableCapture
|
|
{
|
|
private:
|
|
Action<T1>^ delegateWithParams;
|
|
T1 item1;
|
|
|
|
VariableCapture(Action<T1>^ delegateWithParams, T1 item1)
|
|
{
|
|
this->delegateWithParams = delegateWithParams;
|
|
this->item1 = item1;
|
|
|
|
}
|
|
void RunDelegate()
|
|
{
|
|
this->delegateWithParams(item1);
|
|
}
|
|
|
|
public:
|
|
static Action^ Capture(Action<T1>^ delegateWithParams, T1 item1)
|
|
{
|
|
VariableCapture<T1>^ capture = gcnew VariableCapture<T1>(delegateWithParams, item1);
|
|
return gcnew Action(capture, &VariableCapture<T1>::RunDelegate);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
generic<typename T1,typename T2>
|
|
public ref class VariableCapture2
|
|
{
|
|
private:
|
|
Action<T1,T2>^ delegateWithParams;
|
|
T1 item1;
|
|
T2 item2;
|
|
|
|
VariableCapture2(Action<T1,T2>^ delegateWithParams, T1 item1, T2 item2)
|
|
{
|
|
this->delegateWithParams = delegateWithParams;
|
|
this->item1 = item1;
|
|
this->item2 = item2;
|
|
|
|
}
|
|
void RunDelegate()
|
|
{
|
|
this->delegateWithParams(item1,item2);
|
|
}
|
|
|
|
public:
|
|
static Action^ Capture(Action<T1,T2>^ delegateWithParams, T1 item1, T2 item2)
|
|
{
|
|
VariableCapture2<T1,T2>^ capture = gcnew VariableCapture2<T1,T2>(delegateWithParams, item1,item2);
|
|
return gcnew Action(capture, &VariableCapture2<T1,T2>::RunDelegate);
|
|
}
|
|
};
|
|
|
|
|
|
/*Usage
|
|
Action^ delegateWithoutParams = VariableCapture<int, String^>::Capture(delegateWithParams, 7, "foo");
|
|
*/ |