From 835487e4669dec49f809de5df08832bc6206b37d Mon Sep 17 00:00:00 2001 From: David Hall Date: Tue, 29 Oct 2019 10:43:18 -0600 Subject: [PATCH] Added GetOrderedFields Type extension method. --- Core/Extensions/ReflectionExtensions.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Core/Extensions/ReflectionExtensions.cs b/Core/Extensions/ReflectionExtensions.cs index 7da49233..57cc1944 100644 --- a/Core/Extensions/ReflectionExtensions.cs +++ b/Core/Extensions/ReflectionExtensions.cs @@ -247,6 +247,13 @@ namespace Vanara.Extensions bool ImplIEnumT(Type t) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>); } + /// Gets the fields of a structure with sequential layout in the order in which they appear in memory. + /// The type of the structure. + /// The binding flags. + /// An ordered sequence of instances representing the fields in the structure. + public static IEnumerable GetOrderedFields(this Type type, BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance) => + type.GetFields(bindingFlags).Select(fi => (System.Runtime.InteropServices.Marshal.OffsetOf(type, fi.Name).ToInt64(), fi)).OrderBy(t => t.Item1).Select(t => t.fi); + /// Gets a named field value from an object. /// The expected type of the field to be returned. /// Name of the field.