AutoLazy
AutoLazy copied to clipboard
Null handling.
This is a really cool project.
If the result of the body of the property is null, then the body of the property is called each time.
This could be fixed using a slightly different implementation:
// begin - fields added by the post-compile step
private static Lazy<Settings> _settings = new Lazy<Settings>(() => __Settings);
public static Settings Settings { get { return _settings.Value; } }
// end
// Property is made private and name changed somehow
private static Settings __Settings
{
get
{
using (var fs = File.Open("settings.xml", FileMode.Open))
{
var serializer = new XmlSerializer(typeof(Settings));
return (Settings)serializer.Deserialize(fs);
}
}
}
Ah yes. This is a problem for reference types. For value types i wrap the value in a container class I think (it's been a while since I wrote this). I could do the same for reference types but Lazy doesn't have the behavior I want. I want it to:
- Guarantee that the function is called exactly once.
- Retry should an exception be thrown (Lazy will save and rethrow the exception if one occurs on the first time.)