private static ToolboxItem CreateToolboxItem(Type type, AssemblyName name)
        {
            ToolboxItemAttribute attribute1 = (ToolboxItemAttribute)
                                              TypeDescriptor.GetAttributes(type)[typeof(ToolboxItemAttribute)];
            ToolboxItem item1 = null;

            if (!attribute1.IsDefaultAttribute())
            {
                Type type1 = attribute1.ToolboxItemType;
                if (type1 != null)
                {
                    item1 = CreateToolboxItemInstance(type1, type);
                    if (item1 != null)
                    {
                        if (name != null)
                        {
                            item1.AssemblyName = name;
                        }
                    }
                }
            }
            if (((item1 == null) && (attribute1 != null)) && !attribute1.Equals(ToolboxItemAttribute.None))
            {
                item1 = new ToolboxItem(type);
                if (name != null)
                {
                    item1.AssemblyName = name;
                }
            }
            return(item1);
        }
Exemple #2
0
        //Gets the toolbox item associated with a component type
        internal static ToolboxItem GetToolboxItem(Type toolType)
        {
            if (toolType == null)
            {
                throw new ArgumentNullException("toolType");
            }

            ToolboxItem item = null;

            if ((toolType.IsPublic || toolType.IsNestedPublic) && typeof(IComponent).IsAssignableFrom(toolType) && !toolType.IsAbstract)
            {
                ToolboxItemAttribute toolboxItemAttribute = (ToolboxItemAttribute)TypeDescriptor.GetAttributes(toolType)[typeof(ToolboxItemAttribute)];
                if (toolboxItemAttribute != null && !toolboxItemAttribute.IsDefaultAttribute())
                {
                    Type itemType = toolboxItemAttribute.ToolboxItemType;
                    if (itemType != null)
                    {
                        // First, try to find a constructor with Type as a parameter.  If that
                        // fails, try the default constructor.
                        ConstructorInfo ctor = itemType.GetConstructor(new Type[] { typeof(Type) });
                        if (ctor != null)
                        {
                            item = (ToolboxItem)ctor.Invoke(new object[] { toolType });
                        }
                        else
                        {
                            ctor = itemType.GetConstructor(new Type[0]);
                            if (ctor != null)
                            {
                                item = (ToolboxItem)ctor.Invoke(new object[0]);
                                item.Initialize(toolType);
                            }
                        }
                    }
                }
                else if (!toolboxItemAttribute.Equals(ToolboxItemAttribute.None))
                {
                    item = new ToolboxItem(toolType);
                }
            }
            else if (typeof(ToolboxItem).IsAssignableFrom(toolType))
            {
                // if the type *is* a toolboxitem, just create it..
                //
                try
                {
                    item = (ToolboxItem)Activator.CreateInstance(toolType, true);
                }
                catch
                {
                }
            }

            return(item);
        }
        public void NonDefaultType()
        {
            ToolboxItemAttribute attr = new ToolboxItemAttribute(false);

            Assert.AreEqual(string.Empty, attr.ToolboxItemTypeName, "#1");
            Assert.IsNull(attr.ToolboxItemType, "#2");
            Assert.AreEqual(false, attr.IsDefaultAttribute(), "#3");

            Assert.AreEqual(string.Empty, ToolboxItemAttribute.None.ToolboxItemTypeName, "#4");
            Assert.IsNull(ToolboxItemAttribute.None.ToolboxItemType, "#5");
            Assert.AreEqual(false, ToolboxItemAttribute.None.IsDefaultAttribute(), "#6");
        }
        public void DefaultType()
        {
            ToolboxItemAttribute attr = new ToolboxItemAttribute(true);

            Type toolboxItemType = typeof(global::System.Drawing.Design.ToolboxItem);

            Assert.AreEqual(toolboxItemType.AssemblyQualifiedName, attr.ToolboxItemTypeName, "#1");
            Assert.AreEqual(toolboxItemType, attr.ToolboxItemType, "#2");
            Assert.AreEqual(true, attr.IsDefaultAttribute(), "#3");
            Assert.AreEqual(attr.ToolboxItemTypeName.GetHashCode(), attr.GetHashCode(), "#4");

            Assert.AreEqual(toolboxItemType.AssemblyQualifiedName, ToolboxItemAttribute.Default.ToolboxItemTypeName, "#5");
            Assert.AreEqual(toolboxItemType, ToolboxItemAttribute.Default.ToolboxItemType, "#2");
            Assert.AreEqual(true, ToolboxItemAttribute.Default.IsDefaultAttribute(), "#3");
            Assert.AreEqual(ToolboxItemAttribute.Default.ToolboxItemTypeName.GetHashCode(), attr.GetHashCode(), "#4");
        }
Exemple #5
0
        public static ToolboxItem GetToolboxItem(System.Type toolType, bool nonPublic)
        {
            ToolboxItem item = null;

            if (toolType == null)
            {
                throw new ArgumentNullException("toolType");
            }
            if (((nonPublic || toolType.IsPublic) || toolType.IsNestedPublic) && (typeof(IComponent).IsAssignableFrom(toolType) && !toolType.IsAbstract))
            {
                ToolboxItemAttribute attribute = (ToolboxItemAttribute)TypeDescriptor.GetAttributes(toolType)[typeof(ToolboxItemAttribute)];
                if (!attribute.IsDefaultAttribute())
                {
                    System.Type toolboxItemType = attribute.ToolboxItemType;
                    if (toolboxItemType != null)
                    {
                        ConstructorInfo constructor = toolboxItemType.GetConstructor(new System.Type[] { typeof(System.Type) });
                        if ((constructor != null) && (toolType != null))
                        {
                            return((ToolboxItem)constructor.Invoke(new object[] { toolType }));
                        }
                        constructor = toolboxItemType.GetConstructor(new System.Type[0]);
                        if (constructor != null)
                        {
                            item = (ToolboxItem)constructor.Invoke(new object[0]);
                            item.Initialize(toolType);
                        }
                    }
                    return(item);
                }
                if (!attribute.Equals(ToolboxItemAttribute.None) && !toolType.ContainsGenericParameters)
                {
                    item = new ToolboxItem(toolType);
                }
                return(item);
            }
            if (typeof(ToolboxItem).IsAssignableFrom(toolType))
            {
                item = (ToolboxItem)Activator.CreateInstance(toolType, true);
            }
            return(item);
        }
        // If the type represents a toolbox item, return an instance of the type;
        // otherwise, return null.
        private ToolboxItem CreateToolboxItem(Type possibleItem)
        {
            // A toolbox item must implement IComponent and must not be abstract.
            if (!typeof(IComponent).IsAssignableFrom(possibleItem) ||
                possibleItem.IsAbstract)
            {
                return(null);
            }

            // A toolbox item must have a constructor that takes a parameter of
            // type Type or a constructor that takes no parameters.
            if (null == possibleItem.GetConstructor(new Type[] { typeof(Type) }) &&
                null == possibleItem.GetConstructor(new Type[0]))
            {
                return(null);
            }

            ToolboxItem item = null;

            // Check the custom attributes of the candidate type and attempt to
            // create an instance of the toolbox item type.
            AttributeCollection attribs =
                TypeDescriptor.GetAttributes(possibleItem);
            ToolboxItemAttribute tba =
                attribs[typeof(ToolboxItemAttribute)] as ToolboxItemAttribute;

            if (tba != null && !tba.Equals(ToolboxItemAttribute.None))
            {
                if (!tba.IsDefaultAttribute())
                {
                    // This type represents a custom toolbox item implementation.
                    Type            itemType = tba.ToolboxItemType;
                    ConstructorInfo ctor     =
                        itemType.GetConstructor(new Type[] { typeof(Type) });
                    if (ctor != null && itemType != null)
                    {
                        item = (ToolboxItem)ctor.Invoke(new object[] { possibleItem });
                    }
                    else
                    {
                        ctor = itemType.GetConstructor(new Type[0]);
                        if (ctor != null)
                        {
                            item = (ToolboxItem)ctor.Invoke(new object[0]);
                            item.Initialize(possibleItem);
                        }
                    }
                }
                else
                {
                    // This type represents a default toolbox item.
                    item = new ToolboxItem(possibleItem);
                }
            }
            if (item == null)
            {
                throw new ApplicationException("Unable to create a ToolboxItem " +
                                               "object from " + possibleItem.FullName + ".");
            }

            // Update the display name of the toolbox item and add the item to
            // the list.
            DisplayNameAttribute displayName =
                attribs[typeof(DisplayNameAttribute)] as DisplayNameAttribute;

            if (displayName != null && !displayName.IsDefaultAttribute())
            {
                item.DisplayName = displayName.DisplayName;
            }

            return(item);
        }
Exemple #7
0
        public static ToolboxItem GetToolboxItem(Type toolType, bool nonPublic = false)
        {
            if (toolType == null)
            {
                throw new ArgumentNullException("toolType");
            }

            ToolboxItem toolboxItem = null;

            if ((nonPublic || toolType.IsPublic || toolType.IsNestedPublic) && typeof(IComponent).IsAssignableFrom(toolType) && !toolType.IsAbstract)
            {
                AttributeCollection  attributeCollection  = TypeDescriptor.GetAttributes(toolType);
                ToolboxItemAttribute toolboxItemAttribute = (ToolboxItemAttribute)attributeCollection[typeof(ToolboxItemAttribute)];
                if (!toolboxItemAttribute.IsDefaultAttribute())
                {
                    Type toolboxItemType = toolboxItemAttribute.ToolboxItemType;
                    if (toolboxItemType != (Type)null)
                    {
                        ConstructorInfo constructor = toolboxItemType.GetConstructor(new Type[1]
                        {
                            typeof(Type)
                        });
                        if (constructor != (ConstructorInfo)null && toolType != (Type)null)
                        {
                            toolboxItem = (ToolboxItem)constructor.Invoke(new object[1]
                            {
                                toolType
                            });
                        }
                        else
                        {
                            constructor = toolboxItemType.GetConstructor(new Type[0]);
                            if (constructor != (ConstructorInfo)null)
                            {
                                toolboxItem = (ToolboxItem)constructor.Invoke(new object[0]);
                                toolboxItem.Initialize(toolType);
                            }
                        }
                    }
                }
                else if (!toolboxItemAttribute.Equals(ToolboxItemAttribute.None) && !toolType.ContainsGenericParameters)
                {
                    toolboxItem = new ToolboxItem(toolType);
                }

                if (toolboxItem != null)
                {
                    toolboxItem.Properties.Add("GroupName", toolType.GetCustomAttributesData()
                                               .FirstOrDefault(data => data.AttributeType.FullName == "DevExpress.Utils.ToolboxTabNameAttribute")?.ConstructorArguments[0].Value ?? "Devexpress");
                }

                goto IL_0137;
            }

            if (typeof(ToolboxItem).IsAssignableFrom(toolType))
            {
                toolboxItem = (ToolboxItem)Activator.CreateInstance(toolType, true);
                goto IL_0137;
            }

IL_0137:

            return(toolboxItem);
        }