MemberInfo - GetDeclaraction
A MemberInfo extension method that gets a declaraction.
public static void Main() { MemberInfo [] members = typeof(FieldModel<int>).GetMember("P*", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (var member in members) { //C# Extension Method: MemberInfo - GetDeclaraction 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.Reflection; public static partial class Extensions { /// <summary>A MemberInfo extension method that gets a declaraction.</summary> /// <param name="this">The @this to act on.</param> /// <returns>The declaraction.</returns> public static string GetDeclaraction(this MemberInfo @this) { switch (@this.MemberType) { case MemberTypes.Field: return ((FieldInfo) @this).GetDeclaraction(); case MemberTypes.Property: return ((PropertyInfo) @this).GetDeclaraction(); case MemberTypes.Constructor: return ((ConstructorInfo) @this).GetDeclaraction(); case MemberTypes.Method: return ((MethodInfo) @this).GetDeclaraction(); case MemberTypes.TypeInfo: return ((Type) @this).GetDeclaraction(); case MemberTypes.NestedType: return ((Type) @this).GetDeclaraction(); case MemberTypes.Event: return ((EventInfo) @this).GetDeclaraction(); default: return null; } } }