/// <summary> /// Extract the value from the resource dictionnary using the given value and behavior /// </summary> /// <param name="value">value used to find the resource</param> /// <param name="behavior">behavior of the finder</param> /// <returns></returns> private object ExtractResource(object value, ResourceSelectorBehavior behavior) { if (Resources == null) return null; switch (behavior) { // Simplest way : value as key case ResourceSelectorBehavior.ValueAsKey: if (value == null) { if (Resources.Contains(string.Empty)) return Resources[string.Empty]; return null; } var asIProvideKey = value as IProvideResourceKey; if (asIProvideKey != null) { if (Resources.Contains(asIProvideKey.ResourceKey)) return Resources[asIProvideKey.ResourceKey]; return null; } var asString = value as string; if (asString == null) asString = System.Convert.ToString(value, CultureInfo.InvariantCulture); if (asString != null) { if (Resources.Contains(asString)) return Resources[asString]; return null; } return null; // More tricky to accept inheritance case ResourceSelectorBehavior.ValueTypeAsKey: Type type = value.GetType(); // This particular Linq query is fabulous. Yes, I'm proud of it return GetAncestorsAndSelf(type) .Select(t => ExtractResourceFromType(t)) .Where(res => res != null) .FirstOrDefault(); } return null; }
protected virtual void OnBehaviorChanged(ResourceSelectorBehavior oldBehavior, ResourceSelectorBehavior newBehavior) { }