gpt4 book ai didi

c# - 方法名称 ToString()

转载 作者:行者123 更新时间:2023-11-30 20:08:49 26 4
gpt4 key购买 nike

GetCached(BLCustomer.GetAll, "GetAll");

其中 "GetAll" 是 session key 。我怎样才能做这样的事情?

GetCached(BLCustomer.GetAll, BLCustomer.GetAll.ToString());

更新:

其他世界我想从方法名 BLCustomer.GetAll 中获取字符串 "GetAll"(不是客户的名称,而是方法的名称) ()

我想用这样的东西

GetCached(BLCustomer.GetSingle, BLCustomer.GetSingle.ToString());

代替

GetCached(BLCustomer.GetSingle, "GetSingle");

避免硬编码方法名称。

最佳答案

像这样更改 GetCached:

ReturnType GetCached(SomeFunc f)
{
var methodname = f.Method.Name;
// add rest of code
}

假设:

我猜 GetCached 目前实际上看起来像这样:

T GetCached<T>(Func<T> accessor, string name)
{
...
}

鉴于 accessor 已经是委托(delegate),名称可以如上所示确定。

否则,我的建议将行不通。

以上还假定 BLCustomer.GetSingle 是一个方法(实例或静态应该没问题)。

调用将是:

var r = GetCached(BLCustomer.GetSingle); // delegate implicitly created

关于c# - 方法名称 ToString(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com