/// <summary> /// This adds the delegate value as a listener to when this event is fired /// by the component, invoking the addOnXXX method. /// </summary> public override void AddEventHandler(object component, Delegate value) { FillMethods(); if (component != null) { ISite?site = GetSite(component); IComponentChangeService?changeService = null; // Announce that we are about to change this component if (site != null) { changeService = (IComponentChangeService?)site.GetService(typeof(IComponentChangeService)); } if (changeService != null) { try { changeService.OnComponentChanging(component, this); } catch (CheckoutException coEx) { if (coEx == CheckoutException.Canceled) { return; } throw; } changeService.OnComponentChanging(component, this); } bool shadowed = false; if (site != null && site.DesignMode) { // Events are final, so just check the class if (EventType != value.GetType()) { throw new ArgumentException(SR.Format(SR.ErrorInvalidEventHandler, Name)); } IDictionaryService?dict = (IDictionaryService?)site.GetService(typeof(IDictionaryService)); if (dict != null) { Delegate?eventdesc = (Delegate?)dict.GetValue(this); eventdesc = Delegate.Combine(eventdesc, value); dict.SetValue(this, eventdesc); shadowed = true; } } if (!shadowed) { _addMethod !.Invoke(component, new[] { value }); } // Now notify the change service that the change was successful. changeService?.OnComponentChanged(component, this, null, value); } }
/// <summary> /// This will remove the delegate value from the event chain so that /// it no longer gets events from this component. /// </summary> public override void RemoveEventHandler(object component, Delegate value) { FillMethods(); if (component != null) { ISite?site = GetSite(component); IComponentChangeService?changeService = null; // Announce that we are about to change this component if (site != null) { changeService = (IComponentChangeService?)site.GetService(typeof(IComponentChangeService)); } if (changeService != null) { try { changeService.OnComponentChanging(component, this); } catch (CheckoutException coEx) { if (coEx == CheckoutException.Canceled) { return; } throw; } changeService.OnComponentChanging(component, this); } bool shadowed = false; if (site != null && site.DesignMode) { IDictionaryService?dict = (IDictionaryService?)site.GetService(typeof(IDictionaryService)); if (dict != null) { Delegate?del = (Delegate?)dict.GetValue(this); del = Delegate.Remove(del, value); dict.SetValue(this, del); shadowed = true; } } if (!shadowed) { _removeMethod !.Invoke(component, new[] { value }); } // Now notify the change service that the change was successful. changeService?.OnComponentChanged(component, this, null, value); } }
/// <summary> /// Retrieves the component name from the site. /// </summary> internal static string?GetComponentName(object?instance) { IComponent?comp = instance as IComponent; ISite? site = comp?.Site; if (site != null) { INestedSite?nestedSite = site as INestedSite; return((nestedSite?.FullName) ?? site.Name); } return(null); }
// Gets the selection service from the control's site internal static ISelectionService?GetSelectionService(Control ctl) { ISite?site = ctl.Site; if (site is not null) { ISelectionService?selectionService = site.GetService <ISelectionService>(); Debug.Assert(selectionService is not null, "service must implement ISelectionService"); return(selectionService); } return(null); }
public virtual void Add(IComponent?component, string?name) { lock (_syncObj) { if (component == null) { return; } ISite?site = component.Site; if (site != null && site.Container == this) { return; } if (_sites == null) { _sites = new ISite[4]; } else { // Validate that new components have either a null name or a unique one. ValidateName(component, name); if (_sites.Length == _siteCount) { ISite[] newSites = new ISite[_siteCount * 2]; Array.Copy(_sites, newSites, _siteCount); _sites = newSites; } } site?.Container !.Remove(component); ISite newSite = CreateSite(component, name); _sites[_siteCount++] = newSite; component.Site = newSite; _components = null; } }
/// <summary> /// Converts the given value object to the reference type using the specified context and arguments. /// </summary> public override object?ConvertTo(ITypeDescriptorContext?context, CultureInfo?culture, object?value, Type destinationType) { if (destinationType == typeof(string)) { if (value != null) { // Try the reference service first. if (context?.GetService(typeof(IReferenceService)) is IReferenceService refSvc) { string?name = refSvc.GetName(value); if (name != null) { return(name); } } // Now see if this is an IComponent. if (!Marshal.IsComObject(value) && value is IComponent comp) { ISite? site = comp.Site; string?name = site?.Name; if (name != null) { return(name); } } // Couldn't find it. return(string.Empty); } return(s_none); } return(base.ConvertTo(context, culture, value, destinationType)); }
public virtual void ApplyResources(object value, string objectName, CultureInfo?culture) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (objectName == null) { throw new ArgumentNullException(nameof(objectName)); } if (culture == null) { culture = CultureInfo.CurrentUICulture; } // The general case here will be to always use the same culture, so optimize for // that. The resourceSets hashtable uses culture as a key. It's value is // a sorted dictionary that contains ALL the culture values (so it traverses up // the parent culture chain) for that culture. This means that if ApplyResources // is called with different cultures there could be some redundancy in the // table, but it allows the normal case of calling with a single culture to // be much faster. // // The reason we use a SortedDictionary here is to ensure the resources are applied // in an order consistent with codedom deserialization. SortedList <string, object?>?resources; if (_resourceSets == null) { _resourceSets = new Hashtable(); resources = FillResources(culture, out _); _resourceSets[culture] = resources; } else { resources = (SortedList <string, object?>?)_resourceSets[culture]; if (resources == null || (resources.Comparer.Equals(StringComparer.OrdinalIgnoreCase) != IgnoreCase)) { resources = FillResources(culture, out _); _resourceSets[culture] = resources; } } BindingFlags flags = BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance; if (IgnoreCase) { flags |= BindingFlags.IgnoreCase; } bool componentReflect = false; if (value is IComponent) { ISite?site = ((IComponent)value).Site; if (site != null && site.DesignMode) { componentReflect = true; } } foreach (KeyValuePair <string, object?> kvp in resources) { // See if this key matches our object. string key = kvp.Key; if (IgnoreCase) { if (string.Compare(key, 0, objectName, 0, objectName.Length, StringComparison.OrdinalIgnoreCase) != 0) { continue; } } else { if (string.CompareOrdinal(key, 0, objectName, 0, objectName.Length) != 0) { continue; } } // Character after objectName.Length should be a "." or a '-', or else we should continue. int idx = objectName.Length; if (key.Length <= idx || (key[idx] != '.' && key[idx] != '-')) { continue; } // Bypass type descriptor if we are not in design mode. TypeDescriptor does an attribute // scan which is quite expensive. string propName = key.Substring(idx + 1); if (componentReflect) { PropertyDescriptor?prop = TypeDescriptor.GetProperties(value).Find(propName, IgnoreCase); if (prop != null && !prop.IsReadOnly && (kvp.Value == null || prop.PropertyType.IsInstanceOfType(kvp.Value))) { prop.SetValue(value, kvp.Value); } } else { PropertyInfo?prop; try { prop = value.GetType().GetProperty(propName, flags); } catch (AmbiguousMatchException) { // Looks like we ran into a conflict between a declared property and an inherited one. // In such cases, we choose the most declared one. Type?t = value.GetType(); do { prop = t.GetProperty(propName, flags | BindingFlags.DeclaredOnly); t = t.BaseType; } while (prop == null && t != null && t != typeof(object)); } if (prop != null && prop.CanWrite && (kvp.Value == null || prop.PropertyType.IsInstanceOfType(kvp.Value))) { prop.SetValue(value, kvp.Value, null); } } } }