c# – 如何在强类型的接口上引用方法名称
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何在强类型的接口上引用方法名称,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2880字,纯文字阅读大概需要5分钟。
内容图文
![c# – 如何在强类型的接口上引用方法名称](/upload/InfoBanner/zyjiaocheng/784/2fdde577739341e29e467ac20b317888.jpg)
很抱歉,如果已经回答了这个问题,但我认为我实际上缺乏正式的教育来正确地提出这个问题,因此也没有正确的标准来成功地搜索它.
我有一个API,有几个调用几乎相同的东西,但使用不同的方法作用于不同的输入对象,但总是形成相同的接口.我想采取削减&从API方法调用进程中粘贴方面,以便公共代码在所有方法调用中完成相同的操作.我已经设法使用泛型为输入和输出对象获得一个有效的解决方案,并且正在引用要从字符串调用的方法名称.我想引用强类型方法而不是基于字符串的方法,因此在重新分解时重命名方法名称不会使方法名称的“魔术”字符串等待在运行时爆炸.
下面是我想要实现的非常简化的版本.
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;
}
}
解决方法:
方法名称的“magic”字符串更改为委托上的委托
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# – 如何在强类型的接口上引用方法名称全部内容,希望文章能够帮你解决c# – 如何在强类型的接口上引用方法名称所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。