Tom Gullen Tom Gullen - 1 year ago 91
C# Question

Pass function as parameter

I have a function:

private static PrivateMessage GetPM()
using (var db = new DBContext())
var q = CompiledQueries.GetPMByID(db, messageID);
if (q == null) return null;
return new PrivateMessage(q);

I wish to pass this function as a parameter:

var pm = cacheObj.SetIfNotExists(GetPM);

is defined as:

public T SetIfNotExists<T>(Func<T> getCachableObjectFunction)

What do I need to modify so that I can pass
as a parameter in
? EG:

private static PrivateMessage GetPM(int messageID)

Answer Source

Wrap and typecast your output is the easiest way.

var pm = cacheObj.SetIfNotExists(() => GetPM(1));

And change GetPM header to:

private static PrivateMessage GetPM(int messageID)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download