gpt4 book ai didi

c# - 如何在强类型接口(interface)上引用方法名称

转载 作者:行者123 更新时间:2023-11-30 12:14:24 24 4
gpt4 key购买 nike

很抱歉,如果这个问题已经得到解答,但我认为我实际上缺乏正规教育来正确地提出这个问题,因此也缺乏正确的标准来成功搜索它。

我有一个 API,它有多个调用,它们做几乎相同的事情,但使用不同的方法作用于不同的输入对象,但始终形成相同的接口(interface)。我想将剪切和粘贴方面从 API 方法调用过程中移除,以便公共(public)代码在所有方法调用中都以相同的方式完成。我已经设法为输入和输出对象使用泛型获得了一个有效的解决方案,并且正在引用要从字符串调用的方法名称。我希望对方法的引用是强类型的而不是基于字符串的,这样在重构时重命名方法名称就不会留下方法名称的“魔术”字符串等待在运行时爆炸。

下面是我想要实现的一个非常简化的版本。

class ARequest { };
class AResponse { };
class BRequest { };
class BResponse { };

interface IWorker
{
AResponse DoA(ARequest aRequest);
BResponse DoB(BRequest bRequest);
}
class Worker : IWorker
{
public AResponse DoA(ARequest aRequest)
{
return new AResponse();
}
public BResponse DoB(BRequest bRequest)
{
return new BResponse();
}
}

class Program
{
static void Main(string[] args)
{
// current concrete copy & paste implementation
var a1 = API.DoA(new ARequest { });
var b1 = API.DoB(new BRequest { });
// new generic implementation
var a2 = API.DoA2(new ARequest { });
var b2 = API.DoB2(new BRequest { });
}
}

static class API
{
// current concrete copy & paste implementation
public static AResponse DoA(ARequest aRequest)
{
// lots of common code for logging & preperation
var worker = GetWorker();
return worker.DoA(aRequest);
}
public static BResponse DoB(BRequest bRequest)
{
// lots of common code for logging & preperation
var worker = GetWorker();
return worker.DoB(bRequest);
}
private static IWorker GetWorker()
{
return new Worker();
}
// new generic implementation Attempt
public static AResponse DoA2(ARequest aRequest)
{
return DoGen<ARequest, AResponse>(aRequest, "DoA"); // how to make references to DoA and DoB methods on the IWorker strongly typed?
}
public static BResponse DoB2(BRequest bRequest)
{
return DoGen<BRequest, BResponse>(bRequest, "DoB"); // how to make references to DoA and DoB methods on the IWorker strongly typed?
}
public static TResponse DoGen<TRequest, TResponse>(TRequest requestObj, string methodname)
where TRequest : class
where TResponse : class
{
// lots of common code for logging & preperation
var worker = GetWorker();
var mi = worker.GetType().GetMethod(methodname);
var result = mi.Invoke(worker, new Object[] { requestObj });
return result as TResponse;
}

}

最佳答案

方法名称的“魔术”字符串更改为委托(delegate)上的委托(delegate)

  public static AResponse DoA2(ARequest aRequest)
{
return DoGen<ARequest, AResponse>(aRequest, worker => worker.DoA);
}
public static BResponse DoB2(BRequest bRequest)
{
return DoGen<BRequest, BResponse>(bRequest, worker => worker.DoB);
}
public static TResponse DoGen<TRequest, TResponse>(TRequest requestObj,
Func<IWorker, Func<TRequest, TResponse>> methodRef)
where TRequest : class
where TResponse : class
{
// lots of common code for logging & preparation
var worker = GetWorker();
var method = methodRef(worker);

return method(requestObj);
}

关于c# - 如何在强类型接口(interface)上引用方法名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9566557/

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