[英]C# Wrapper Function Delegate
我在整个应用程序中都有以下模式,使用模拟来创建Server对象以检索数据。
public ActionResult Index(){
Index model = new Index();
using (new Impersonation("domain", "username", "password")){
Server server = new Server("serverInstance");
model.ApplicationList = server.GetApplications();
model.Details = server.GetDetails();
}
}
我想知道是否可以将其转换为包装函数。 所以可以这样称呼
SecureManager.PerformOperation("domain", "username", "password", server => server
{
server.GetApplications();
server.GetDetails();
....
});
目标是仅使用“模拟”块中的“服务器”对象。
是的,你可以这么做。
void /*SecureManager.*/PerformOperation(
string domain, string username, string password, Action<Server> action)
{
using (new Impersonation(domain, username, password))
{
action(new Server("serverInstance"));
}
}
但这并不能真正解决“仅在Impersonation块内使用Server对象”的问题,因为可以很容易地从lambda中“泄漏”该对象:
Server leakedServer = null;
SecureManager.PerformOperation("domain", "username", "password",
server =>
{
leakedServer = server;
....
});
leakedServer.GetApplications();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.