だから私は Func コールバックを渡す関数を持っています。また、オブジェクトに対して投影を実行できるように、ある種の選択投影を追加したいと考えています。つまり、データベース呼び出しを 1 回だけ実行します。関数は次のようになります。
public T Get<T>(string id, Func<T> getItemCallback) where T : class
{
item = getItemCallback();
if (item != null)
{
doSomeThing(item);
// Here I would like to call something else that is
// expecting a specific type. Is there way to pass in a
// dynamic selector?
doSomethingElse(item.Select(x => new CustomType { id = x.id, name = x.name }).ToList());
}
return item;
}
void doSomethingElse(List<CustomType> custom)
{
....
}
レメは、私が現在これをどのように呼び出しているかを示しています。
public List<MyDataSet> GetData(string keywords, string id)
{
return _myObject.Get(
id,
() => db.GetDataSet(keywords, id).ToList());
// Perhaps I could add another parameter here to
// handled the projection ????
}
リードのおかげで、私はそれを理解しました...次のようになります:
public T Get<T>(string id, Func<T> getItemCallback, Func<T, List<CustomType>> selector) where T : class
{
item = getItemCallback();
if (item != null)
{
doSomething(item);
var custom = selector(item);
if (custom != null)
{
doSomethingElse(custom);
}
}
return item;
}
呼び出しは次のようになります。
public List<MyDataSet> GetData(string keywords, string id)
{
return _myObject.Get(
id,
() => db.GetDataSet(keywords, id).ToList(),
x => x.Select(d => new CustomType { id = d.ReferenceId, name = d.Name })
.ToList());
}