Linqpad:如果修改、保存并重新运行查询文件,LINQPad.Util.Cache 会引发错误。 “无法加载类型”错误
将令人惊叹的 LinqPad 与 C# 脚本结合使用。使用 Util.Cache
存储在同一脚本中声明的自定义类时出现错误。请问除了重启LinqPad之外如何解决?
重现错误:
- 将任何字符添加到注释
- 保存文件
- 再次运行
错误引发:
Cannot load type 'UserQuery+IPerson' from cache.
Data
(0 items)
HelpLink null
HResult -2146233088
InnerException null
Message Cannot load type 'UserQuery+IPerson' from cache.
Source LINQPad.Runtime
StackTrace at LINQPad.Util.Cache[T](Func`1 dataFetcher, String key, Boolean& fromCache)
at LINQPad.Util.Cache[T](Func`1 dataFetcher, String key)
at UserQuery.Main(), line 3
TargetSite
RuntimeMethodInfo•••
CacheConverter.CanConvert (Type sourceType, Type targetType, HashSet<TypeType> visitedTypes)
Linqpad 查询代码
这是正在运行的代码:
void Main()
{
IPerson p = Util.Cache<IPerson>(()=>new Person("Bob"), "person_bob9");
p.Dump();
// add any char after here, save re-run, error thrown: a
}
interface IPerson{string Name {get;init;}}
class Person:IPerson
{
public Person(string name)=> Name = name;
public string Name { get; init; }
}
注释
可能会发生这种情况,因为每次修改文件并运行时,Person 类都会被 linqpad 编译成 dll。当 Person 的实例存储在缓存中时,这可能与当前的 Person 类型声明不同,因此会出现错误。
Using the amazing LinqPad with a C# script. Get an error using the Util.Cache
storing a custom class declared in the same script. How to resolve, other than restarting LinqPad please?
Reproduce error:
- Add any char to the comment
- save file
- run again
Error raised:
Cannot load type 'UserQuery+IPerson' from cache.
Data
(0 items)
HelpLink null
HResult -2146233088
InnerException null
Message Cannot load type 'UserQuery+IPerson' from cache.
Source LINQPad.Runtime
StackTrace at LINQPad.Util.Cache[T](Func`1 dataFetcher, String key, Boolean& fromCache)
at LINQPad.Util.Cache[T](Func`1 dataFetcher, String key)
at UserQuery.Main(), line 3
TargetSite
RuntimeMethodInfo•••
CacheConverter.CanConvert (Type sourceType, Type targetType, HashSet<TypeType> visitedTypes)
Linqpad query code
Here is the code being run:
void Main()
{
IPerson p = Util.Cache<IPerson>(()=>new Person("Bob"), "person_bob9");
p.Dump();
// add any char after here, save re-run, error thrown: a
}
interface IPerson{string Name {get;init;}}
class Person:IPerson
{
public Person(string name)=> Name = name;
public string Name { get; init; }
}
Notes
Maybe this happens because the Person class is compiled by linqpad into a dll each time the file is modified and run. When an instance of Person is stored in a cache, this might differ from the current Person type declaration, hence the error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜它适用于函数
i guess it works with functions