コード例 #1
0
ファイル: ToolboxItem.cs プロジェクト: axelheer/dotnet-corefx
        public virtual void Initialize(Type type)
        {
            CheckUnlocked();

            if (type != null)
            {
                TypeName = type.FullName;
                AssemblyName assemblyName = type.Assembly.GetName(true);
                if (type.Assembly.GlobalAssemblyCache)
                {
                    assemblyName.CodeBase = null;
                }

                Dictionary <string, AssemblyName> parents = new Dictionary <string, AssemblyName>();
                Type parentType = type;
                while (parentType != null)
                {
                    AssemblyName policiedname = parentType.Assembly.GetName(true);

                    AssemblyName aname = GetNonRetargetedAssemblyName(type, policiedname);

                    if (aname != null && !parents.ContainsKey(aname.FullName))
                    {
                        parents[aname.FullName] = aname;
                    }
                    parentType = parentType.BaseType;
                }

                AssemblyName[] parentAssemblies = new AssemblyName[parents.Count];
                int            i = 0;
                foreach (AssemblyName an in parents.Values)
                {
                    parentAssemblies[i++] = an;
                }

                this.DependentAssemblies = parentAssemblies;

                AssemblyName = assemblyName;
                DisplayName  = type.Name;

                //if the Type is a reflectonly type, these values must be set through a config object or manually
                //after construction.
                if (!type.Assembly.ReflectionOnly)
                {
                    object[] companyattrs = type.Assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true);
                    if (companyattrs != null && companyattrs.Length > 0)
                    {
                        AssemblyCompanyAttribute company = companyattrs[0] as AssemblyCompanyAttribute;
                        if (company != null && company.Company != null)
                        {
                            Company = company.Company;
                        }
                    }

                    //set the description based off the description attribute of the given type.
                    DescriptionAttribute descattr = (DescriptionAttribute)TypeDescriptor.GetAttributes(type)[typeof(DescriptionAttribute)];
                    if (descattr != null)
                    {
                        this.Description = descattr.Description;
                    }

                    ToolboxBitmapAttribute attr = (ToolboxBitmapAttribute)TypeDescriptor.GetAttributes(type)[typeof(ToolboxBitmapAttribute)];
                    if (attr != null)
                    {
                        Bitmap itemBitmap = attr.GetImage(type, false) as Bitmap;
                        if (itemBitmap != null)
                        {
                            // Original bitmap is used when adding the item to the Visual Studio toolbox
                            // if running on a machine with HDPI scaling enabled.
                            OriginalBitmap = attr.GetOriginalBitmap();
                            if ((itemBitmap.Width != iconWidth || itemBitmap.Height != iconHeight))
                            {
                                itemBitmap = new Bitmap(itemBitmap, new Size(iconWidth, iconHeight));
                            }
                        }
                        Bitmap = itemBitmap;
                    }

                    bool      filterContainsType = false;
                    ArrayList array = new ArrayList();
                    foreach (Attribute a in TypeDescriptor.GetAttributes(type))
                    {
                        ToolboxItemFilterAttribute ta = a as ToolboxItemFilterAttribute;
                        if (ta != null)
                        {
                            if (ta.FilterString.Equals(TypeName))
                            {
                                filterContainsType = true;
                            }
                            array.Add(ta);
                        }
                    }

                    if (!filterContainsType)
                    {
                        array.Add(new ToolboxItemFilterAttribute(TypeName));
                    }

                    Filter = (ToolboxItemFilterAttribute[])array.ToArray(typeof(ToolboxItemFilterAttribute));
                }
            }
        }