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);
        }
Exemple #3
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);
        }
Exemple #4
0
        //temporary method until we get a UI and some form of persistence)
        public void PopulateFromAssembly(Assembly assembly)
        {
            Type[] types = assembly.GetTypes();

            foreach (Type t in types)
            {
                if (t.IsAbstract || t.IsNotPublic)
                {
                    continue;
                }

                if (t.GetConstructor(new Type[] {}) == null)
                {
                    continue;
                }

                AttributeCollection atts = TypeDescriptor.GetAttributes(t);

                bool containsAtt = false;
                foreach (Attribute a in atts)
                {
                    if (a.GetType() == typeof(ToolboxItemAttribute))
                    {
                        containsAtt = true;
                    }
                }
                if (!containsAtt)
                {
                    continue;
                }

                ToolboxItemAttribute tba = (ToolboxItemAttribute)atts[typeof(ToolboxItemAttribute)];
                if (tba.Equals(ToolboxItemAttribute.None))
                {
                    continue;
                }
                //FIXME: fix WebControlToolboxItem
                Type toolboxItemType = typeof(ToolboxItem);                 //(tba.ToolboxItemType == null) ? typeof (ToolboxItem) : tba.ToolboxItemType;

                string category = "General";

                if (t.IsSubclassOf(typeof(System.Web.UI.WebControls.BaseValidator)))
                {
                    category = "Validation";
                }
                else if (t.Namespace == "System.Web.UI.HtmlControls" && t.IsSubclassOf(typeof(System.Web.UI.HtmlControls.HtmlControl)))
                {
                    category = "Html Elements";
                }
                else if (t.IsSubclassOf(typeof(System.Web.UI.WebControls.BaseDataList)))
                {
                    category = "Data Controls";
                }
                else if (t.IsSubclassOf(typeof(System.Web.UI.WebControls.WebControl)))
                {
                    category = "Web Controls";
                }

                AddToolboxItem((ToolboxItem)Activator.CreateInstance(toolboxItemType, new object[] { t }), category);
            }
            OnToolboxChanged();
        }
        // 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 #6
0
        IList <ItemToolboxNode> IExternalToolboxLoader.Load(string filename)
        {
            TargetRuntime          runtime = Runtime.SystemAssemblyService.CurrentRuntime;
            List <ItemToolboxNode> list    = new List <ItemToolboxNode> ();

            System.Reflection.Assembly scanAssem;
            try
            {
                if (runtime is MsNetTargetRuntime)
                {
                    scanAssem = System.Reflection.Assembly.ReflectionOnlyLoadFrom(filename);
                }
                else
                {
                    scanAssem = System.Reflection.Assembly.LoadFile(filename);
                }
            }
            catch (Exception ex)
            {
                MonoDevelop.Core.LoggingService.LogError("ToolboxItemToolboxLoader: Could not load assembly '"
                                                         + filename + "'", ex);
                return(list);
            }

            SystemPackage package = runtime.AssemblyContext.GetPackageFromPath(filename);

            //need to initialise if this if out of the main process
            //in order to be able to load icons etc.
            if (!initialized)
            {
                Gtk.Application.Init();
                initialized = true;
            }

            //detect the runtime version
            MonoDevelop.Core.ClrVersion clrVersion = MonoDevelop.Core.ClrVersion.Default;
            byte[] corlibKey = new byte[] { 0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89 };
            //the other system.{...} key:
            //{ 0xb0, 0x3f, 0x5f, 0x7f, 0x11, 0xd5, 0x0a, 0x3a };
            foreach (System.Reflection.AssemblyName an in scanAssem.GetReferencedAssemblies())
            {
                if (an.Name == "mscorlib" && byteArraysEqual(corlibKey, an.GetPublicKeyToken()))
                {
                    if (an.Version == new Version(2, 0, 0, 0))
                    {
                        clrVersion = MonoDevelop.Core.ClrVersion.Net_2_0;
                        break;
                    }
                    else if (an.Version == new Version(1, 0, 5000, 0))
                    {
                        clrVersion = MonoDevelop.Core.ClrVersion.Net_1_1;
                        break;
                    }
                }
            }

            if (clrVersion == MonoDevelop.Core.ClrVersion.Default)
            {
                MonoDevelop.Core.LoggingService.LogError("ToolboxItemToolboxLoader: assembly '"
                                                         + filename + "' references unknown runtime version.");
                return(list);
            }

            Type[] types = scanAssem.GetTypes();

            foreach (Type t in types)
            {
                //skip inaccessible types
                if (t.IsAbstract || !t.IsPublic || !t.IsClass)
                {
                    continue;
                }

                //get the ToolboxItemAttribute if present
                object[] atts = t.GetCustomAttributes(typeof(ToolboxItemAttribute), true);
                if (atts == null || atts.Length == 0)
                {
                    continue;
                }

                ToolboxItemAttribute tba = (ToolboxItemAttribute)atts[0];
                if (tba.Equals(ToolboxItemAttribute.None) || tba.ToolboxItemType == null)
                {
                    continue;
                }

                // Technically CategoryAttribute shouldn't be used for this purpose (intended for properties in
                // the PropertyGrid) but I can see no harm in doing this.
                string cat = null;
                atts = t.GetCustomAttributes(typeof(CategoryAttribute), true);
                if (atts != null && atts.Length > 0)
                {
                    cat = ((CategoryAttribute)atts[0]).Category;
                }

                try
                {
                    ItemToolboxNode node = GetNode(t, tba, cat, package != null? filename : null, clrVersion);
                    if (node != null)
                    {
                        // Make sure this item is only shown for the correct runtime
                        node.ItemFilters.Add(new ToolboxItemFilterAttribute("TargetRuntime." + runtime.Id, ToolboxItemFilterType.Require));
                        list.Add(node);
                    }
                }
                catch (Exception ex)
                {
                    MonoDevelop.Core.LoggingService.LogError(
                        "Unhandled error in toolbox node loader '" + GetType().FullName
                        + "' with type '" + t.FullName
                        + "' in assembly '" + scanAssem.FullName + "'",
                        ex);
                }
            }

            return(list);// Load (scanAssem);
        }
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);
        }