Esempio n. 1
0
        public void Ctor_DisplayName(string displayName, bool expectedIsDefaultAttribute)
        {
            var attribute = new DisplayNameAttribute(displayName);

            Assert.Equal(displayName, attribute.DisplayName);
            Assert.Equal(expectedIsDefaultAttribute, attribute.IsDefaultAttribute());
        }
Esempio n. 2
0
        public void Ctor_Default()
        {
            var attribute = new DisplayNameAttribute();

            Assert.Equal(string.Empty, attribute.DisplayName);
            Assert.True(attribute.IsDefaultAttribute());
        }
        public void Default()
        {
            DisplayNameAttribute dn = DisplayNameAttribute.Default;

            Assert.IsNotNull(dn.DisplayName, "#1");
            Assert.AreEqual(string.Empty, dn.DisplayName, "#2");
            Assert.IsTrue(dn.IsDefaultAttribute(), "#3");
        }
        public void Constructor0()
        {
            DisplayNameAttribute dn = new DisplayNameAttribute();

            Assert.IsNotNull(dn.DisplayName, "#1");
            Assert.AreEqual(string.Empty, dn.DisplayName, "#2");
            Assert.IsTrue(dn.IsDefaultAttribute(), "#3");
        }
        public void TestDefaultAttribute()
        {
            Type tc1t = tc1.GetType();
            DisplayNameAttribute dn = GetAttribute(tc1t);

            Assert.IsNotNull(dn, "#1_1");
            Assert.IsTrue(dn.IsDefaultAttribute(), "#1_2");

            dn = GetAttribute(tc1t, "Property1", MemberTypes.Property);
            Assert.IsNotNull(dn, "#1_3");
            Assert.IsTrue(dn.IsDefaultAttribute(), "#1_4");

            dn = GetAttribute(tc1t, "Method1", MemberTypes.Method);
            Assert.IsNotNull(dn, "#1_5");
            Assert.IsTrue(dn.IsDefaultAttribute(), "#1_6");

            Type tc2t = tc2.GetType();

            dn = GetAttribute(tc2t);
            Assert.IsNotNull(dn, "#2_1");
            Assert.IsFalse(dn.IsDefaultAttribute(), "#2_2");

            dn = GetAttribute(tc2t, "Property2", MemberTypes.Property);
            Assert.IsNotNull(dn, "#2_3");
            Assert.IsFalse(dn.IsDefaultAttribute(), "#2_4");

            dn = GetAttribute(tc2t, "Method2", MemberTypes.Method);
            Assert.IsNotNull(dn, "#2_5");
            Assert.IsFalse(dn.IsDefaultAttribute(), "#2_6");

            Type tc3t = tc3.GetType();

            dn = GetAttribute(tc3t);
            Assert.IsNotNull(dn, "#3_1");
            Assert.IsFalse(dn.IsDefaultAttribute(), "#3_2");

            dn = GetAttribute(tc3t, "Property3", MemberTypes.Property);
            Assert.IsNotNull(dn, "#3_3");
            Assert.IsFalse(dn.IsDefaultAttribute(), "#3_4");

            dn = GetAttribute(tc3t, "Method3", MemberTypes.Method);
            Assert.IsNotNull(dn, "#3_5");
            Assert.IsFalse(dn.IsDefaultAttribute(), "#3_6");
        }
        //</Snippet10>

        /////////////////////////////////////////////////////////////////////////////
        // Overriden Package Implementation

        // ...
        protected override void Initialize()
        {
            Trace.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                          "Entering Initialize() of: {0}", this.ToString()));
            base.Initialize();

            // Add our command handlers for menu (commands must exist in the .vsct file)
            OleMenuCommandService mcs =
                GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

            if (null != mcs)
            {
                // Create the command for the menu item.
                CommandID menuCommandID = new CommandID(
                    GuidList.guidItemConfigurationCmdSet,
                    (int)PkgCmdIDList.cmdidMyCommand);
                MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
                mcs.AddCommand(menuItem);
            }

            //<Snippet12>
            // Use the toolbox service to get a list of all toolbox items in
            // this assembly.
            ToolboxItemList = new ArrayList(
                ToolboxService.GetToolboxItems(this.GetType().Assembly, ""));
            if (null == ToolboxItemList)
            {
                throw new ApplicationException(
                          "Unable to generate a toolbox item listing for " +
                          this.GetType().FullName);
            }

            // Update the display name of each toolbox item in the list.
            Assembly thisAssembly = this.GetType().Assembly;

            foreach (ToolboxItem item in ToolboxItemList)
            {
                Type underlyingType         = thisAssembly.GetType(item.TypeName);
                AttributeCollection attribs =
                    TypeDescriptor.GetAttributes(underlyingType);
                DisplayNameAttribute displayName =
                    attribs[typeof(DisplayNameAttribute)] as DisplayNameAttribute;
                if (displayName != null && !displayName.IsDefaultAttribute())
                {
                    item.DisplayName = displayName.DisplayName;
                }
            }
            //</Snippet12>
        }
        public void Constructor1()
        {
            DisplayNameAttribute dn = new DisplayNameAttribute(string.Empty);

            Assert.IsNotNull(dn.DisplayName, "#A1");
            Assert.AreEqual(string.Empty, dn.DisplayName, "#A2");
            Assert.IsTrue(dn.IsDefaultAttribute(), "#A3");

            dn = new DisplayNameAttribute(null);
            Assert.IsNull(dn.DisplayName, "#B1");
            Assert.IsFalse(dn.IsDefaultAttribute(), "#B2");

            dn = new DisplayNameAttribute("category");
            Assert.IsNotNull(dn.DisplayName, "#C1");
            Assert.AreEqual("category", dn.DisplayName, "#C2");
            Assert.IsFalse(dn.IsDefaultAttribute(), "#C3");
        }
        // 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);
        }