Object - GetField
A T extension method that searches for the public field with the specified name.
public static void Main() { //C# Extension Method: Object - GetField FieldInfo constField = typeof(FieldModel<int>).GetField("ConstField", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); FieldInfo eventField = typeof(FieldModel<int>).GetField("EventField", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); FieldInfo internalField = typeof(FieldModel<int>).GetField("InternalField", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); Console.WriteLine(constField.GetDeclaraction()); Console.WriteLine(eventField.GetDeclaraction()); Console.WriteLine(internalField.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>A T extension method that searches for the public field with the specified name.</summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="name">The string containing the name of the data field to get.</param> /// <returns> /// An object representing the field that matches the specified requirements, if found; /// otherwise, null. /// </returns> public static FieldInfo GetField<T>(this T @this, string name) { return @this.GetType().GetField(name); } /// <summary> /// A T extension method that searches for the specified field, using the specified /// binding constraints. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="name">The string containing the name of the data field to get.</param> /// <param name="bindingAttr"> /// A bitmask comprised of one or more BindingFlags that specify how the /// search is conducted. /// </param> /// <returns> /// An object representing the field that matches the specified requirements, if found; /// otherwise, null. /// </returns> public static FieldInfo GetField<T>(this T @this, string name, BindingFlags bindingAttr) { return @this.GetType().GetField(name, bindingAttr); } }