Linqpad:如果修改、保存并重新运行查询文件,LINQPad.Util.Cache 会引发错误。 “无法加载类型”错误

发布于 2025-01-13 22:05:13 字数 1321 浏览 2 评论 0原文

将令人惊叹的 LinqPad 与 C# 脚本结合使用。使用 Util.Cache 存储在同一脚本中声明的自定义类时出现错误。请问除了重启LinqPad之外如何解决?

重现错误:

  1. 将任何字符添加到注释
  2. 保存文件
  3. 再次运行

错误引发:

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:

  1. Add any char to the comment
  2. save file
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不吻晚风 2025-01-20 22:05:13

我猜它适用于函数


void Main()
{
    var p = Util.Cache<Person>(() => AddPerson("uuuu"), "cached_user").Dump();
    p.Dump();
    string s = "";

}

interface IPerson
{
    string Name { get; init; }
}
[Serializable]
public class Person : IPerson
{
    public Person(string name) => Name = name;
    public string Name { get; init; }
}

public Person AddPerson(string Name)
{
    //Thread.Sleep(1000);
    return new Person(Name);
}

i guess it works with functions


void Main()
{
    var p = Util.Cache<Person>(() => AddPerson("uuuu"), "cached_user").Dump();
    p.Dump();
    string s = "";

}

interface IPerson
{
    string Name { get; init; }
}
[Serializable]
public class Person : IPerson
{
    public Person(string name) => Name = name;
    public string Name { get; init; }
}

public Person AddPerson(string Name)
{
    //Thread.Sleep(1000);
    return new Person(Name);
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文