Пример #1
0
        public static void AddProperty <T>(this PropertyTarget target, string key, string displayName, string description, T initialValue, T[] standardValues = null)
        {
            DynTypeDescriptor.InstallTypeDescriptor(target);
            var td = DynTypeDescriptor.GetTypeDescriptor(target);

            if (td == null)
            {
                throw new Exception("Could not load type descriptor");
            }


            var pd = new DynPropertyDescriptor(target.GetType(), key, typeof(T), initialValue
                                               , new BrowsableAttribute(true)
                                               , new DisplayNameAttribute(displayName)
                                               , new DescriptionAttribute(description)
                                               );

            if (standardValues != null && standardValues.Length > 0)
            {
                pd.Attributes.Add(new TypeConverterAttribute(typeof(DynStandardValueConverter)), true);
                foreach (var value in standardValues)
                {
                    var sv = new DynStandardValue(value);
                    sv.DisplayName = value.ToString();
                    pd.StandardValues.Add(sv);
                }
            }

            td.GetProperties().Add(pd);
        }
        public static bool InstallTypeDescriptor(object instance)
        {
            CleanUpRef();
            if ((from DictionaryEntry de in TypeDescriptorTable select de.Key as WeakReference).Any(wr => wr != null && (wr.IsAlive && instance.Equals(wr.Target))))
            {
                return(false); // because already installed
            }

            // will have to install the provider and create a new entry in the hash table
            var parentProvider = TypeDescriptor.GetProvider(instance);
            var parentCtd      = parentProvider.GetTypeDescriptor(instance);
            var ourCtd         = new DynTypeDescriptor(parentCtd, instance);
            var ourProvider    = new TypeDescriptionProvider(parentProvider, ourCtd);

            TypeDescriptor.AddProvider(ourProvider, instance);
            var weakRef = new WeakReference(instance, true);

            TypeDescriptorTable.Add(weakRef, ourCtd);
            return(true);
        }