System.ComponentModel.TypeDescriptor.GetAssociation C# (CSharp) Method

GetAssociation() private method

private GetAssociation ( Type type, object primary ) : object
type System.Type
primary object
return object
        public static object GetAssociation(Type type, object primary)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (primary == null)
            {
                throw new ArgumentNullException(nameof(primary));
            }

            object associatedObject = primary;

            if (!type.GetTypeInfo().IsInstanceOfType(primary))
            {
                // Check our association table for a match.
                //
                Hashtable assocTable = s_associationTable;
                if (assocTable != null)
                {
                    IList associations = (IList)assocTable[primary];
                    if (associations != null)
                    {
                        lock (associations)
                        {
                            for (int idx = associations.Count - 1; idx >= 0; idx--)
                            {
                                // Look for an associated object that has a type that
                                // matches the given type.
                                //
                                WeakReference weakRef = (WeakReference)associations[idx];
                                object secondary = weakRef.Target;
                                if (secondary == null)
                                {
                                    associations.RemoveAt(idx);
                                }
                                else if (type.GetTypeInfo().IsInstanceOfType(secondary))
                                {
                                    associatedObject = secondary;
                                }
                            }
                        }
                    }
                }

#if FEATURE_IDESIGNERHOST
                // Not in our table.  We have a default association with a designer 
                // if that designer is a component.
                //
                if (associatedObject == primary)
                {
                    IComponent component = primary as IComponent;
                    if (component != null)
                    {
                        ISite site = component.Site;

                        if (site != null && site.DesignMode)
                        {
                            IDesignerHost host = site.GetService(typeof(IDesignerHost)) as IDesignerHost;
                            if (host != null)
                            {
                                object designer = host.GetDesigner(component);

                                // We only use the designer if it has a compatible class.  If we
                                // got here, we're probably hosed because the user just passed in
                                // an object that this PropertyDescriptor can't munch on, but it's
                                // clearer to use that object instance instead of it's designer.
                                //
                                if (designer != null && type.GetTypeInfo().IsInstanceOfType(designer))
                                {
                                    associatedObject = designer;
                                }
                            }
                        }
                    }
                }
#endif
            }

            return associatedObject;
        }

Usage Example

Example #1
0
 protected static object GetInvokee(Type componentClass, object component)
 {
     if (componentClass == null)
     {
         throw new ArgumentNullException("componentClass");
     }
     if (component == null)
     {
         throw new ArgumentNullException("component");
     }
     return(TypeDescriptor.GetAssociation(componentClass, component));
 }
All Usage Examples Of System.ComponentModel.TypeDescriptor::GetAssociation