protected T CachedComputation <T>(Func <T> computation, string[] dependentOn, [CallerMemberName] string propertyName = null) { if (m_cachedComputationProperties == null) { m_cachedComputationProperties = new Dictionary <string, CachedComputationProperty>(); } CachedComputationProperty prop; if (!m_cachedComputationProperties.TryGetValue(propertyName, out prop)) { prop = new CachedComputationProperty(this, propertyName, delegate { return(computation()); }); m_cachedComputationProperties[propertyName] = prop; foreach (var dependentOnProperty in dependentOn) { ListenToProperty(dependentOnProperty, prop.NotifyDependentOnValueChanged); } } object val = prop.GetValue(); if (val == null) { return(default(T)); } return((T)val); }
protected T CachedComputation <T>(Func <T> computation, string[] dependentOn, [CallerMemberName] string propertyName = null) { // Have to initialize this here since this method gets called before any class initializers are performed (not sure why exactly, probably because it's used in property values in parent class) // But there shouldn't be any cross-threading concerns since that's part of the overall initialization anyways too if (m_cachedComputationPropertiesLock == null) { m_cachedComputationPropertiesLock = new object(); } if (propertyName == null) { // This should theoretically never be null, but seems like Android, when running in the widget at least, it ends up being null? // Adding this check to confirm. throw new ArgumentNullException(nameof(propertyName)); } CachedComputationProperty prop; lock (m_cachedComputationPropertiesLock) { if (m_cachedComputationProperties == null) { m_cachedComputationProperties = new Dictionary <string, CachedComputationProperty>(); } if (!m_cachedComputationProperties.TryGetValue(propertyName, out prop)) { prop = new CachedComputationProperty(this, propertyName, delegate { return(computation()); }); m_cachedComputationProperties[propertyName] = prop; ListenToProperties(dependentOn, prop.NotifyDependentOnValueChanged); } } object val = prop.GetValue(); if (val == null) { return(default(T)); } return((T)val); }