public void Initialize_Locked_Null() { ToolboxItem item = new ToolboxItem(); item.Lock(); item.Initialize(null); }
public void Initialize_Locked_Null() { Assert.Throws <InvalidOperationException>(() => { ToolboxItem item = new ToolboxItem(); item.Lock(); item.Initialize(null); }); }
//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); }
private static ToolboxItem CreateToolboxItemInstance(Type toolboxItemType, Type toolType) { ToolboxItem item1 = null; ConstructorInfo info1 = toolboxItemType.GetConstructor(new Type[] { typeof(Type) }); if (info1 != null) { return((ToolboxItem)info1.Invoke(new object[] { toolType })); } info1 = toolboxItemType.GetConstructor(new Type[0]); if (info1 != null) { item1 = (ToolboxItem)info1.Invoke(new object[0]); item1.Initialize(toolType); } return(item1); }
// 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); }
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); }