/// <summary> /// Method to cleanup resources for callers which register themselves as owner. /// </summary> /// <param name="res">Element to be cleaned up or disposed.</param> /// <param name="checkOwner">Owner reference to check for. This method will only clean up the given element if the /// specified <paramref name="checkOwner"/> is the owner of the given element or if the element doesn't have an owner.</param> public static void CleanupAndDisposeResourceIfOwner(object res, object checkOwner) { IUnmodifiableResource resource = res as IUnmodifiableResource; if (resource == null || resource.Owner == null || ReferenceEquals(resource.Owner, checkOwner)) { TryCleanupAndDispose_NoCheckOwner(res); } }
/// <summary> /// Sets the owner of the given resource to the given <paramref name="owner"/>, if the given resource implements the /// <see cref="IUnmodifiableResource"/> interface. The owner is only set if no owner is set yet, except if <paramref name="force"/> is /// set to <c>true</c>. /// </summary> /// <remarks> /// Containers like <see cref="ResourceDictionary"/> or <see cref="Setter"/> set themselves as owner of their contents. /// That has two implications: /// <list type="bullet"> /// <item>The owner of a resource is responsible for the disposal of the resource. A resource with an owner cannot live longer than its owner.</item> /// <item>A resource with an owner is not copied. Instead, the <see cref="CopyMpfObject"/> method will return the original reference.</item> /// </list> /// That works only for resources which implement the <see cref="IUnmodifiableResource"/> interface. Those resources are unmodifiable, i.e. /// they are not "personalized" to their owner so it is safe to reuse their reference. /// So if a container sets itself as owner of its contents, it can optimize the performance if the contents are very often of type /// <see cref="IUnmodifiableResource"/>. /// </remarks> /// <param name="res">Resource to set the owner.</param> /// <param name="owner">Owner to be set.</param> /// <param name="force">If set to <c>false</c> and the <paramref name="res">resource</paramref> has already an owner, nothing happens. /// Else, the owner of the resource will be set.</param> public static void SetOwner(object res, object owner, bool force) { IUnmodifiableResource resource = res as IUnmodifiableResource; if (resource != null && (resource.Owner == null || force)) { resource.Owner = owner; } }
/// <summary> /// Method to cleanup resources for callers which don't register themselves as owner. /// </summary> /// <param name="maybeUIElementOrDisposable">Element to be cleaned up or disposed.</param> public static void TryCleanupAndDispose(object maybeUIElementOrDisposable) { IUnmodifiableResource resource = maybeUIElementOrDisposable as IUnmodifiableResource; if (resource != null && resource.Owner != null) { // Optimize disposal for unmodifiable resources: They are only disposed by their parent ResourceDictionary return; } TryCleanupAndDispose_NoCheckOwner(maybeUIElementOrDisposable); }
public static bool CopyMpfObject(object source, out object target) { target = null; if (source == null) { return(true); } Type t = source.GetType(); if (t == typeof(Vector2)) { Vector2 vec = (Vector2)source; Vector2 result = new Vector2 { X = vec.X, Y = vec.Y }; target = result; return(true); } if (t == typeof(Vector3)) { Vector3 vec = (Vector3)source; Vector3 result = new Vector3 { X = vec.X, Y = vec.Y, Z = vec.Z }; target = result; return(true); } if (t == typeof(Vector4)) { Vector4 vec = (Vector4)source; Vector4 result = new Vector4 { X = vec.X, Y = vec.Y, W = vec.W, Z = vec.Z }; target = result; return(true); } if (source is IUnmodifiableResource) { IUnmodifiableResource resource = (IUnmodifiableResource)source; if (resource.Owner != null) { target = source; return(true); } } return(false); }
public static void RegisterUnmodifiableResourceDuringParsingProcess(IUnmodifiableResource resource, IParserContext context) { IEnumerator <ElementContextInfo> enumer = ((IEnumerable <ElementContextInfo>)context.ContextStack).GetEnumerator(); if (!enumer.MoveNext()) { return; } if (!enumer.MoveNext()) { return; } ResourceDictionary rd = enumer.Current.Instance as ResourceDictionary; if (rd != null) { MPF.SetOwner(resource, rd, false); } }