public static void AttachObjectExtender(IExtensibleObject extensibleObject, Extension.IExtensionProvider extensionProvider) { Guard.ArgumentNotNull(extensibleObject, "extensibleObject"); Guard.ArgumentNotNull(extensionProvider, "extensionProvider"); ExtensionProviderHelper extensionProviderHelper = new ExtensionProviderHelper(extensibleObject); object extender = extensionProviderHelper.CreateObjectExtender(extensionProvider, extensibleObject as ModelElement); extensionProviderHelper.SetObjectExtender(extender); }
/// <summary> /// Creates the object extender. /// </summary> /// <param name="extensionProvider">The extension provider.</param> /// <returns></returns> public object CreateObjectExtender(Extension.IExtensionProvider extensionProvider, ModelElement modelElement) { Guard.ArgumentNotNull(extensionProvider, "extensionProvider"); Type objectExtenderType = GetObjectExtenderType(extensionProvider, this.extensibleObject.GetType()); if (objectExtenderType == null) { return(null); } if (extensibleObject.ObjectExtenderContainer == null) { CreateObjectExtenderContainer(); } IObjectExtender objectExtender = GetObjectExtender(extensionProvider) as IObjectExtender; // reset container to avoid cloning containers on copy/paste operations if (objectExtender != null && objectExtender.ModelElement != null && objectExtender.ModelElement != modelElement) { CreateObjectExtenderContainer(); objectExtender = null; } if (objectExtender == null) { try { objectExtender = Activator.CreateInstance(objectExtenderType) as IObjectExtender; this.extensibleObject.ObjectExtenderContainer.ObjectExtenders.Add(objectExtender); } catch (Exception ex) { Logger.Write(ex); } } if (objectExtender.ModelElement == null) { IObjectExtenderInternal objectExtenderInternal = objectExtender as IObjectExtenderInternal; if (objectExtenderInternal != null) { objectExtenderInternal.ModelElement = modelElement; } } return(objectExtender); }
/// <summary> /// Gets the object extender. /// </summary> /// <param name="extensionProvider">The extension provider.</param> /// <returns></returns> public object GetObjectExtender(Extension.IExtensionProvider extensionProvider) { Guard.ArgumentNotNull(extensionProvider, "extensionProvider"); Type objectExtenderType = GetObjectExtenderType(extensionProvider, this.extensibleObject.GetType()); if (this.extensibleObject.ObjectExtenderContainer == null) { return(null); } foreach (object objectExtender in this.extensibleObject.ObjectExtenderContainer.ObjectExtenders) { if (objectExtender.GetType() == objectExtenderType) { return(objectExtender); } } return(null); }
/// <summary> /// Gets the type of the object extender associated to an object. /// </summary> /// <param name="extensionProvider">The extension provider.</param> /// <param name="objectToExtend">The object to extend.</param> /// <returns></returns> public static Type GetObjectExtenderType(Extension.IExtensionProvider extensionProvider, Type objectToExtend) { Guard.ArgumentNotNull(extensionProvider, "extensionProvider"); foreach (Type type in extensionProvider.ObjectExtenders) { ObjectExtenderAttribute att = ReflectionHelper.GetAttribute <ObjectExtenderAttribute>(type); if (att != null) { if (att.ObjectToExtend == objectToExtend || att.ObjectToExtend.IsAssignableFrom(objectToExtend)) { return(type); } } } // If no extender registered for the specified object then return null return(null); }