繁体   English   中英

通过功能作为参数

[英]Pass function as parameter

我有一个功能:

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

我希望将此函数作为参数传递:

var pm = cacheObj.SetIfNotExists(GetPM);

SetIfNotExists定义为:

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

我需要修改什么以便可以将messageID作为参数传递给GetPM() 例如:

private static PrivateMessage GetPM(int messageID)

包装并转换输出是最简单的方法。

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

并将GetPM标头更改为:

private static PrivateMessage GetPM(int messageID)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM