'Linqpad: LINQPad.Util.Cache throws error if query file is modified, saved and re-run. "Cannot load type" error
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.
Solution 1:[1]
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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Power Mouse |
