Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Enum.GetValues()是否使用反射?_C#_.net - Fatal编程技术网

C# Enum.GetValues()是否使用反射?

C# Enum.GetValues()是否使用反射?,c#,.net,C#,.net,Enum.GetValues()是否使用反射 我认为不会,因为在编译时它应该能够获取可能值的列表并存储它们。但我不知道它是否真的这样做了。我认为它应该这样做。下面是执行此任务的代码 // This will return enumValues and enumNames sorted by the values. private void GetEnumData(out string[] enumNames, out Array enumValues) {

Enum.GetValues()是否使用反射


我认为不会,因为在编译时它应该能够获取可能值的列表并存储它们。但我不知道它是否真的这样做了。

我认为它应该这样做。下面是执行此任务的代码

    // 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;
    }

所以我不确定它是如何为enum实现的。

@KennethK。这回答了问题吗?它调用一个可能使用反射的虚拟方法,具体取决于编译器生成枚举类型的方式。当然,如果它根据其记录的行为执行,那么它如何执行也不重要。如果它被证明是一个性能热点,那么它的工作方式也无关紧要,因为你无论如何都不会使用它。@Damien_不相信这一点,但我问的更多是出于好奇,而不是试图解决问题。但除此之外,如果我能提前预测性能热点,那就更好了。大致上,是的。有两种反射代码,一种是经过RuntimeType的通用代码,另一种是使用专用CLR辅助函数的特定代码。后者使用可以从CLR维护的内部类型表示中检索的类型信息。快的那种。这就是Enum.GetValues()所使用的,专用的CLR辅助函数是一个名为getEnumValuesandNames()的QCall。它位于relectinvocation.cpp源文件中。比你想知道的还要多,对不起:)
 abstract public FieldInfo[] GetFields(BindingFlags bindingAttr);