gpt4 book ai didi

c# - 有没有办法保存 LINQPad.Util.Compile 的结果并在很长一段时间内重新运行它

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

我有一个脚本 Web 服务,可以从内容管理系统下载并运行 linqpad .linq 程序。我目前正在做类似下面代码的事情。有没有办法保存 LINQPad.Util.Compile 的结果并将其存储在某个地方,以便我可以使用它直到 .linq 文件发生更改?现在我认为它每次都在重新编译,并产生许多编译文件夹。

public static object DownloadAndRunScript(string scriptFilePath, object args)
{
var compiledScript = LINQPad.Util.Compile(scriptFilePath, true);
var queryExecutorResult = compiledScript.Run(LINQPad.QueryResultFormat.Text, args);
return (object)queryExecutorResult.ReturnValue;
}

最佳答案

您可以很容易地为此编写一个缓存函数。像这样:

static Dictionary<string, Tuple<DateTime, QueryCompilation>> _cache
= new Dictionary<string, System.Tuple<System.DateTime, QueryCompilation>>();

public static QueryCompilation GetCompilation (string scriptFilePath)
{
Tuple<DateTime, LINQPad.ObjectModel.QueryCompilation> entry;
var writeTime = new FileInfo (scriptFilePath).LastWriteTimeUtc;

lock (_cache)
if (_cache.TryGetValue (scriptFilePath, out entry) && entry.Item1 == writeTime)
return entry.Item2;

var compiledScript = LINQPad.Util.Compile (scriptFilePath, true);

lock (_cache)
_cache [scriptFilePath] = Tuple.Create (writeTime, compiledScript);

return compiledScript;
}

关于c# - 有没有办法保存 LINQPad.Util.Compile 的结果并在很长一段时间内重新运行它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35203662/

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