Background:
- I have 1...* objects of the same class (X).
- The objects are given a delegate that fetches data for it.
- All objects of class X uses the same delegate.
- Each object of class X wants a different set of data.
- The SDK I'm using have declared the delegate to have no parameters.
I somehow need to check which object that is calling the delegate and act according to that.
Code section 1: The following section shows a snippet where objects of class X is created. As noted by the comment getRows is defined to be the "callback"
public void getTables() { foreach(X currentTable in mapper.getTables()) { MTables.Add(new X { TableName = currentTable.getName(), GetRows = getRows, //This is the delegate Fields = Fields.ToArray() }); } } Code section 2: Class X declares the delegate like this:
public X.GetRowsHandler GetRows { get; set; } public delegate IEnumerable<QvxDataRow> GetRowsHandler(); Code section 3: Here is pseudo code for function "getRows"
private IEnumerable<QvxDataRow> getRows() { // foreach row belonging to calling instance of class X //yield return row; } Section 3 and section 1 is declared in the same class, using the SDK examples.
I have looked for a solution for the last 5 hours, but I can't wrap my head around delegates. Some previous post on SO suggested that delegare.Caller can be used, but I don't understand how it is used and I'm not even sure it's applicable in this case?
Any suggestion how to deal with this?