Object - GetFields
An object extension method that gets the fields.
public static void Main() { //C# Extension Method: Object - GetFields var fields = typeof(FieldModel<int>).GetFields(); foreach(var field in fields) { Console.WriteLine(field.GetDeclaraction()); } } public class FieldModel<T> { public const int ConstField = 1; public static int StaticField; public readonly int ReadOnlyField = 1; public volatile int VolatileField = 1; public event EventHandler EventField; internal int InternalField; protected internal int ProtectedInternalField; private int PrivateField; protected int ProtectedField; public int PublicField; public T GenericField; }
View Source
using System.Reflection; public static partial class Extensions { /// <summary>An object extension method that gets the fields.</summary> /// <param name="this">The @this to act on.</param> /// <returns>An array of field information.</returns> public static FieldInfo[] GetFields(this object @this) { return @this.GetType().GetFields(); } /// <summary>An object extension method that gets the fields.</summary> /// <param name="this">The @this to act on.</param> /// <param name="bindingAttr">The binding attribute.</param> /// <returns>An array of field information.</returns> public static FieldInfo[] GetFields(this object @this, BindingFlags bindingAttr) { return @this.GetType().GetFields(bindingAttr); } }