Reputation: 624
I'm currently try to learning Reactive programming in C# with trying to convert my old project to Reactive, I use a dll file to connect to a biometric machine, this is some event exposed by the dll, how I can convert this to Observable using Observable.FromEvent?
// I use the event like this,
axCZKEM.OnAttTransactionEx += new _IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM_OnAttTransactionEx);
and this is the generated metadata
#region Assembly Interop.zkemkeeper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// C:\Project\C#\obj\Debug\Interop.zkemkeeper.dll
#endregion
using System.Runtime.InteropServices;
namespace zkemkeeper
{
[ComVisible(false)]
[TypeLibType(16)]
public delegate void _IZKEMEvents_OnAttTransactionExEventHandler(
string EnrollNumber,
int IsInValid,
int AttState,
int VerifyMethod,
int Year,
int Month,
int Day,
int Hour,
int Minute,
int Second,
int WorkCode);
}
Upvotes: 0
Views: 195
Reputation: 3293
Even I am new to Reactive programming, But I will try as there are no other answers, You will need two things here. One, Use a wrapper for your event args.
internal class MyArgs
{
public string EnrollNumber;
public int IsInValid;
public int AttState;
public int VerifyMethod;
public int Year;
public int Month;
public int Day;
public int Hour;
public int Minute;
public int Second;
public int WorkCode;
}
Then use FromEvent with a converter to convert your args into MyArgs.
var observable = Observable.FromEvent<_IZKEMEvents_OnAttTransactionExEventHandler, MyArgs>(
converter => new _IZKEMEvents_OnAttTransactionExEventHandler(
(enrollNumber, isInValid, attState, verifyMethod, year, month, day, hour, minute, second, workCode)
=> converter(new MyArgs
{
EnrollNumber = enrollNumber,
IsInValid = isInValid,
AttState = attState,
VerifyMethod = verifyMethod,
Day=day,
Hour=hour,
Minute=minute,
Month=month,
Second=second,
WorkCode=workCode,
Year=year
})
),
handler => axCZKEM.OnAttTransactionEx += handler,
handler => axCZKEM.OnAttTransactionEx -= handler);
Upvotes: 1