5

Does Enum.GetValues() use reflection?

I don't think it would, since at compile time it should be able to grab the list of possible values and store them. But I don't know if that's what it actually does.

9
  • 2
    You tell me.
    – Kenneth K.
    Jul 27, 2018 at 14:45
  • 3
    @KennethK. Does that answer the question? It calls a virtual method that might use reflection depending on how the enum type is generated by the compiler.
    – D Stanley
    Jul 27, 2018 at 14:47
  • 3
    Also, of course, it shouldn't matter how it does it providing it's performing according to its documented behaviour. If it turns out it's e.g. a performance hotspot, it again doesn't matter how it works because you're not going to use it anyway. Jul 27, 2018 at 14:50
  • 2
    @Damien_The_Unbeliever true, but I was asking more out of curiosity than actually trying to solve a problem. But besides, if I can predict a performance hotspot beforehand, that'd be preferable.
    – Jordak
    Jul 27, 2018 at 14:58
  • 2
    Roughly, yes. There are two kind of reflection code, the general kind that goes through RuntimeType and the specific kind that uses dedicated CLR helper functions. The latter uses type info that can retrieved from the internal type representation that the CLR maintains. The fast kind. That is what Enum.GetValues() uses, the dedicated CLR helper function is a QCall named GetEnumValuesAnd Names(). It is located here, in the relectioninvocation.cpp source file. More than you wanted to know, sorry :) Jul 27, 2018 at 15:57

1 Answer 1

1

I think it should. Below is the code which does the job

    // This will return enumValues and enumNames sorted by the values.
    private void GetEnumData(out string[] enumNames, out Array enumValues)
    {
        Contract.Ensures(Contract.ValueAtReturn<String[]>(out enumNames) != null);
        Contract.Ensures(Contract.ValueAtReturn<Array>(out enumValues) != null);

        FieldInfo[] flds = GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);

        object[] values = new object[flds.Length];
        string[] names = new string[flds.Length];

        for (int i = 0; i < flds.Length; i++)
        {
            names[i] = flds[i].Name;
            values[i] = flds[i].GetRawConstantValue();
        }

        // Insertion Sort these values in ascending order.
        // We use this O(n^2) algorithm, but it turns out that most of the time the elements are already in sorted order and
        // the common case performance will be faster than quick sorting this.
        IComparer comparer = Comparer.Default;
        for (int i = 1; i < values.Length; i++)
        {
            int j = i;
            string tempStr = names[i];
            object val = values[i];
            bool exchanged = false;

            // Since the elements are sorted we only need to do one comparision, we keep the check for j inside the loop.
            while (comparer.Compare(values[j - 1], val) > 0)
            {
                names[j] = names[j - 1];
                values[j] = values[j - 1];
                j--;
                exchanged = true;
                if (j == 0)
                    break;
            }

            if (exchanged)
            {
                names[j] = tempStr;
                values[j] = val;
            }
        }

        enumNames = names;
        enumValues = values;
    }

Pay attention to FieldInfo[] flds = GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); line. It really looks like reflection method. The method GetFields is abstract

 abstract public FieldInfo[] GetFields(BindingFlags bindingAttr);

so I'm not sure how it is implemented for enums.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.