using System; using System.Diagnostics; using System.Linq; using System.Reflection; namespace Scratch { public class ReflectionCaching { public int F1; public int F2; public int F3; static void Main() { Test("F3", "F1", "F2"); } static void Test(params string[] load) { try { Debug.Assert(GC.TryStartNoGCRegion(1024 * 1024 * 32L)); FieldInfo f; foreach (var s in load) { // puts this element into the member cache f = typeof(ReflectionCaching).GetField(s); } // enumerates all members in the declaration order, and marks the member cache as complete Console.WriteLine("Normal order: " + string.Join(" ", typeof(ReflectionCaching).GetFields().Select(x => x.Name))); // member cache is complete, so return it directly Console.Write("Cache order: " + string.Join(" ", typeof(ReflectionCaching).GetFields().Select(x => x.Name))); } finally { GC.EndNoGCRegion(); GC.Collect(); GC.WaitForPendingFinalizers(); } } } }