private static DynStandardValue[] GetStandardValues(Type enumType, BindingFlags flags)
        {
            var arrAttr = new ArrayList();
            var fields  = enumType.GetFields(flags);

            foreach (var fi in fields)
            {
                var sv = new DynStandardValue(Enum.ToObject(enumType, fi.GetValue(null)));
                sv.DisplayName = Enum.GetName(enumType, sv.Value); // by default

                if (fi.GetCustomAttributes(typeof(DynDisplayNameAttribute), false) is DynDisplayNameAttribute[] dna && dna.Length > 0)
                {
                    sv.DisplayName = dna[0].DisplayName;
                }

                if (fi.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false) is System.ComponentModel.DescriptionAttribute[] da && da.Length > 0)
                {
                    sv.Description = da[0].Description;
                }

                if (fi.GetCustomAttributes(typeof(System.ComponentModel.BrowsableAttribute), false) is System.ComponentModel.BrowsableAttribute[] ba && ba.Length > 0)
                {
                    sv.Visible = ba[0].Browsable;
                }

                if (fi.GetCustomAttributes(typeof(System.ComponentModel.ReadOnlyAttribute), false) is System.ComponentModel.ReadOnlyAttribute[] roa && roa.Length > 0)
                {
                    sv.Enabled = !roa[0].IsReadOnly;
                }
                arrAttr.Add(sv);
            }
            var retAttr = arrAttr.ToArray(typeof(DynStandardValue)) as DynStandardValue[];

            return(retAttr);
        }
Beispiel #2
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);
        }
Beispiel #3
0
        private void UpdateStringFromResource(System.ComponentModel.ITypeDescriptorContext context, DynStandardValue sv)
        {
            ResourceAttribute ra = null;

            if (context != null && context.PropertyDescriptor != null)
            {
                ra = (ResourceAttribute)context.PropertyDescriptor.Attributes.Get(typeof(ResourceAttribute));
            }
            if (ra == null)
            {
                ra = (ResourceAttribute)System.ComponentModel.TypeDescriptor.GetAttributes(typeof(bool)).Get(typeof(ResourceAttribute));
            }

            if (ra == null)
            {
                return;
            }

            ResourceManager rm = null;

            // construct the resource manager using the resInfo
            try
            {
                if (String.IsNullOrEmpty(ra.BaseName) == false && String.IsNullOrEmpty(ra.AssemblyFullName) == false)
                {
                    rm = new ResourceManager(ra.BaseName, Assembly.ReflectionOnlyLoad(ra.AssemblyFullName));
                }
                else if (String.IsNullOrEmpty(ra.BaseName) == false)
                {
                    rm = new ResourceManager(ra.BaseName, typeof(bool).Assembly);
                }
                else if (String.IsNullOrEmpty(ra.BaseName) == false)
                {
                    rm = new ResourceManager(ra.BaseName, typeof(bool).Assembly);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
            if (rm == null)
            {
                return;
            }

            // update the display and description string from resource using the resource manager

            string keyName     = ra.KeyPrefix + sv.Value + "_Name"; // display name
            string keyDesc     = ra.KeyPrefix + sv.Value + "_Desc"; // description
            string dispName    = string.Empty;
            string description = string.Empty;

            try
            {
                dispName = rm.GetString(keyName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            if (string.IsNullOrEmpty(dispName) == false)
            {
                sv.DisplayName = dispName;
            }

            try
            {
                description = rm.GetString(keyDesc);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            if (string.IsNullOrEmpty(description) == false)
            {
                sv.Description = description;
            }
        }