System.ComponentModel.EnumConverter.GetStandardValues C# (CSharp) Method

GetStandardValues() public method

public GetStandardValues ( ITypeDescriptorContext context ) : StandardValuesCollection
context ITypeDescriptorContext
return StandardValuesCollection
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
            if (values == null) {
                // We need to get the enum values in this rather round-about way so we can filter
                // out fields marked Browsable(false). Note that if multiple fields have the same value,
                // the behavior is undefined, since what we return are just enum values, not names.

                Type reflectType = TypeDescriptor.GetReflectionType(type);
                if (reflectType == null) {
                    reflectType = type;
                }

                FieldInfo[] fields = reflectType.GetFields(BindingFlags.Public | BindingFlags.Static);
                ArrayList objValues = null;

                if (fields != null && fields.Length > 0) {
                    objValues = new ArrayList(fields.Length);
                }

                if (objValues != null) {
                    foreach (FieldInfo field in fields) {
                        BrowsableAttribute browsableAttr = null;
                        foreach (Attribute attr in field.GetCustomAttributes(typeof(BrowsableAttribute), false)) {
                            browsableAttr = attr as BrowsableAttribute;
                        }
                    
                        if (browsableAttr == null || browsableAttr.Browsable) {
                            object value = null;
    
                            try {
                                if (field.Name != null) {
                                    value = Enum.Parse(type, field.Name);
                                }
                            }
                            catch (ArgumentException) {
                                // Hmm, for some reason, the parse threw. Let us ignore this value.
                            }
    
                            if (value != null) {
                                objValues.Add(value);
                            }
                        }
                    }
                    
                    IComparer comparer = Comparer;
                    if (comparer != null) {
                        objValues.Sort(comparer);
                    }
                }

                Array arr = (objValues != null) ? objValues.ToArray() : null;
                values = new StandardValuesCollection(arr);
            }
            return values;
        }
    

Usage Example

Example #1
0
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int progress = 0;
            optrez.Clear();
            finalRez.Clear();
            alg = new PGA();
            OptimizationModel optModel = new OptimizationModel(func);
            EnumConverter InitialLoadTypeCollection = new EnumConverter(typeof(InitialLoadType));
            EnumConverter EndConditionTypeCollecton = new EnumConverter(typeof(EndCondition));
            EnumConverter MutationTypeCollecton = new EnumConverter(typeof(MutationType));
            EnumConverter SelectionTypeCollection = new EnumConverter(typeof(SelectionType));
            foreach (InitialLoadType il in InitialLoadTypeCollection.GetStandardValues())
            {
                foreach (EndCondition ec in EndConditionTypeCollecton.GetStandardValues())
                {
                    foreach (MutationType mt in MutationTypeCollecton.GetStandardValues())
                    {
                        foreach (SelectionType st in SelectionTypeCollection.GetStandardValues())
                        {
                            double[] f1m = new double[(int)numericUpDown8.Value];
                            double[] f2m = new double[(int)numericUpDown8.Value];
                            double[] fm = new double[(int)numericUpDown8.Value];
                            double[] x1m = new double[(int)numericUpDown8.Value];
                            double[] x2m = new double[(int)numericUpDown8.Value];
                            for (int i = 0; i < (int)numericUpDown8.Value; i++)
                            {
                                AlgorithmSettings settings = new AlgorithmSettings()
                                {
                                    InitialLoadType = il,
                                    OptModel = optModel,
                                    InitialPointCount = (int)numericUpDown1.Value,
                                    SelectionType = st,
                                    EndCondition = ec,
                                    MaxGenerationCount = (int)numericUpDown2.Value,
                                    SurvivedCount = (int)numericUpDown3.Value,
                                    MutationChance = (double)numericUpDown4.Value,
                                    CrossingGenNumber = (int)numericUpDown5.Value,
                                    Tolerance = (double)numericUpDown6.Value,
                                    MutationChanceAfterCrossing = (double)numericUpDown7.Value,
                                    MutationType = mt
                                };
                                alg.Run(settings);
                                double x1 = alg.Best.X1;
                                double x2 = alg.Best.X2;
                                double f1 = alg.Best.F;
                                double f2 = alg.CallCount;
                                double f = GetCriterion(f1, f2);
                                f1m[i] = f1;
                                f2m[i] = f2;
                                fm[i] = f;
                                x1m[i] = x1;
                                x2m[i] = x2;
                                optrez.Add(new OptRezult()
                                {
                                    I = il,
                                    E = ec,
                                    S = st,
                                    M = mt,
                                    F1 = f1,
                                    F2 = f2,
                                    X1 = x1,
                                    X2 = x2,
                                    F = f
                                });
                            }
                            progress++;
                            backgroundWorker1.ReportProgress(progress * 100 / 24);
                            finalRez.Add(new OptRezult()
                            {
                                I = il,
                                E = ec,
                                S = st,
                                M = mt,
                                X1 = x1m.Average(),
                                X2 = x2m.Average(),
                                F1 = f1m.Average(),
                                F2 = f2m.Average(),
                                F = fm.Average()

                            });
                        }
                    }
                }
            }
    }