PropertyInfo - GetSignature
A PropertyInfo extension method that gets a signature.
public static void Main() { PropertyInfo PublicProperty = typeof(PropertyModel<int>).GetProperty("PublicProperty", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); PropertyInfo ProtectedInternalProperty = typeof(PropertyModel<int>).GetProperty("ProtectedInternalProperty", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); PropertyInfo AbstractProperty = typeof(PropertyModel<int>).GetProperty("AbstractProperty", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); //C# Extension Method: PropertyInfo - GetSignature string PublicPropertyResult = PublicProperty.GetSignature(); string ProtectedInternalPropertyResult = ProtectedInternalProperty.GetSignature(); string AbstractPropertyResult = AbstractProperty.GetSignature(); Console.WriteLine(PublicPropertyResult); Console.WriteLine(ProtectedInternalPropertyResult); Console.WriteLine(AbstractPropertyResult); } public class PropertyModel<T> : AbstractPropertyModel { public static int StaticProperty { get; set; } public override int OverrideProperty { get; set; } public virtual int VirtualProperty { get; set; } internal int InternalProperty { get; set; } private int PrivateProperty { get; set; } protected int ProtectedProperty { get; set; } protected internal int ProtectedInternalProperty { get; set; } public int PublicProperty { get; set; } public int PublicGetterPrivateSetterProperty { get; private set; } public int PrivateGetterPublicSetterProperty { private get; set; } public T GenericProperty { get; set; } public T this[T param1, int param2] { get { return param1; }} public override int AbstractProperty { get; set; } } public abstract class AbstractPropertyModel { public abstract int AbstractProperty { get; set; } public virtual int OverrideProperty { get; set; } }
View Source
using System.Linq; using System.Reflection; using System.Text; public static partial class Extensions { /// <summary>A PropertyInfo extension method that gets a declaraction.</summary> /// <param name="this">The @this to act on.</param> /// <returns>The declaraction.</returns> public static string GetSignature(this PropertyInfo @this) { // Example: [Name | Indexer[Type]] var indexerParameter = @this.GetIndexParameters(); if (indexerParameter.Length == 0) { // Name return @this.Name; } var sb = new StringBuilder(); // Indexer sb.Append(@this.Name); sb.Append("["); sb.Append(string.Join(", ", indexerParameter.Select(x => x.ParameterType.GetShortSignature()))); sb.Append("]"); return sb.ToString(); } }