Object - GetMemberPaths
A T extension method that gets member paths.
public static void Main() { //C# Extension Method: Object - GetMemberPaths MemberInfo [] members = typeof(FieldModel<int>).GetMemberPaths(""); //var obj = new FieldModel<int>(); foreach (var member in members) { Console.WriteLine(member.GetDeclaraction()); } } public class FieldModel<T> { #region Modifier public const int ConstField = 1; public static int StaticField; public readonly int ReadOnlyField = 1; public volatile int VolatileField = 1; public event EventHandler EventField; #endregion #region Visibility internal int InternalField; protected internal int ProtectedInternalField; private int PrivateField; protected int ProtectedField; public int PublicField; #endregion #region Generic public T GenericField; #endregion }
View Source
using System; using System.Collections.Generic; using System.Reflection; public static partial class Extensions { /// <summary>A T extension method that gets member paths.</summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="path">Full pathname of the file.</param> /// <returns>An array of member information.</returns> public static MemberInfo[] GetMemberPaths<T>(this T @this, string path) { Type lastType = @this.GetType(); string[] paths = path.Split('.'); var memberPaths = new List<MemberInfo>(); foreach (string item in paths) { PropertyInfo propertyInfo = lastType.GetProperty(item); FieldInfo fieldInfo = lastType.GetField(item); if (propertyInfo != null) { memberPaths.Add(propertyInfo); lastType = propertyInfo.PropertyType; } if (fieldInfo != null) { memberPaths.Add(fieldInfo); lastType = fieldInfo.FieldType; } } return memberPaths.ToArray(); } }