Exemplo n.º 1
0
        public AdvTypeConverter()
        {
            Type t = typeof(TType);

            this.parse        = ParseMethodAttribute.GetParseMethod(t);
            this.descriptions = AdvBrowsableAttribute.GetDispMembers(t);
            if (descriptions != null)
            {
                this.instanceNoArgsCtor = t.GetConstructor(Type.EmptyTypes);
                this.instanceCtor       = InstanceConstructorAttribute.GetConstructor(t, out instanceCtorParamNames);
                if (this.instanceCtor != null)
                {
                    ParameterInfo[] paraminfos = instanceCtor.GetParameters();
                    if (paraminfos.Length == instanceCtorParamNames.Length)
                    {
                        for (int index = 0; index < instanceCtorParamNames.Length; ++index)
                        {
                            string             name       = instanceCtorParamNames[index];
                            PropertyDescriptor descriptor = descriptions.Find(name, false);
                            if (descriptor == null || descriptor.PropertyType != paraminfos[index].ParameterType)
                            {
                                instanceCtor = null;
                                break;
                            }
                        }
                    }
                    else
                    {
                        instanceCtor = null;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static PropertyDescriptorCollection GetDispMembers(Type t)
        {
            string[] order = AdvBrowsableOrderAttribute.GetOrder(t);
            List <PropertyDescriptor> rv = new List <PropertyDescriptor>();

            object[] atts;
            foreach (PropertyInfo info in t.GetProperties())
            {
                atts = info.GetCustomAttributes(typeof(AdvBrowsableAttribute), true);
                if (atts.Length > 0)
                {
                    AdvBrowsableAttribute att = (AdvBrowsableAttribute)atts[0];
                    AdvPropertyDescriptor descriptor;
                    if (att.Name != null)
                    {
                        descriptor = new AdvPropertyDescriptor(att.Name, info);
                    }
                    else
                    {
                        descriptor = new AdvPropertyDescriptor(info);
                    }
                    atts = info.GetCustomAttributes(typeof(DescriptionAttribute), true);
                    if (atts.Length > 0)
                    {
                        DescriptionAttribute att2 = (DescriptionAttribute)atts[0];
                        descriptor.SetDescription(att2.Description);
                    }
                    rv.Add(descriptor);
                }
            }
            foreach (FieldInfo info in t.GetFields())
            {
                atts = info.GetCustomAttributes(typeof(AdvBrowsableAttribute), true);
                if (atts.Length > 0)
                {
                    AdvBrowsableAttribute att = (AdvBrowsableAttribute)atts[0];
                    AdvPropertyDescriptor descriptor;
                    if (att.Name != null)
                    {
                        descriptor = new AdvPropertyDescriptor(att.Name, info);
                    }
                    else
                    {
                        descriptor = new AdvPropertyDescriptor(info);
                    }
                    atts = info.GetCustomAttributes(typeof(DescriptionAttribute), true);
                    if (atts.Length > 0)
                    {
                        DescriptionAttribute att2 = (DescriptionAttribute)atts[0];
                        descriptor.SetDescription(att2.Description);
                    }
                    rv.Add(descriptor);
                }
            }
            if (rv.Count == 0)
            {
                return(null);
            }
            if (order != null)
            {
                return(new PropertyDescriptorCollection(rv.ToArray()).Sort(order));
            }
            return(new PropertyDescriptorCollection(rv.ToArray()));
        }