ParameterInfo - GetSignature
A ParameterInfo extension method that gets a signature.
public static void Main() { MethodInfo GenericMethod = typeof(MethodModel<int>).GetMethod("GenericMethod", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo OutParameterMethod = typeof(MethodModel<int>).GetMethod("OutParameterMethod", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo ParamsParameterMethod = typeof(MethodModel<int>).GetMethod("ParamsParameterMethod", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); Type t = typeof(MethodModel<int>); MethodInfo[] minfo = t.GetMethods(); DisplayParametersInfo(GenericMethod.GetParameters()); DisplayParametersInfo(OutParameterMethod.GetParameters()); DisplayParametersInfo(ParamsParameterMethod.GetParameters()); } private static void DisplayParametersInfo(ParameterInfo[] parameterInfo) { foreach (ParameterInfo p in parameterInfo) { //C# Extension Method: ParameterInfo - GetSignature Console.WriteLine(p.GetSignature()); } } public class MethodModel<T> : AbstractMethodModel { public class Toto<T2> {} public static int StaticMethod() { return 1; } public override int OverrideMethod() { return 1; } public virtual int VirtualMethod() { return 1; } public int PublicMethod() { return 1; } private int PrivateMethod() { return 1; } internal int InternalMethod() { return 1; } protected int ProtectedMethod() { return 1; } protected internal int ProtectedInternalMethod() { return 1; } public int OutParameterMethod(out int i) { i = 1; return 1; } public int RefParameterMethod(ref int i) { return 1; } public int ParamsParameterMethod(params int[] i) { return 1; } public int OptionalParameterMethod(int i = 1) { return 1; } public int InParameterModifierMethod([In] int i) { return 1; } public int OutParameterModifierMethod([Out] int i) { i = 1; return 1; } public int OptionalParameterModifierMethod([Optional] int i) { return 1; } public T GenericTypeMethod(T i) { return i; } public T1 GenericMethod<T1, T2>(T1 i, T2 i2) { return i; } public override int AbstractMethod() { throw new NotImplementedException(); } } public abstract class AbstractMethodModel { public abstract int AbstractMethod(); public abstract int OverrideMethod(); } } public static class ExtensionMethodModel { public static int ExtensionParameterMethod(this int i) { return 1; }
View Source
using System; using System.Collections.Generic; using System.Reflection; using System.Runtime.CompilerServices; using System.Text; public static partial class Extensions { /// <summary>A ParameterInfo 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 ParameterInfo @this) { var sb = new StringBuilder(); @this.GetSignature(sb); return sb.ToString(); } internal static void GetSignature(this ParameterInfo @this, StringBuilder sb) { // retval attribute string typeName; Type elementType = @this.ParameterType.GetElementType(); if (elementType != null) { typeName = @this.ParameterType.Name.Replace(elementType.Name, elementType.GetShortSignature()); } else { typeName = @this.ParameterType.GetShortSignature(); } if (@this.IsOut) { if (typeName.Contains("&")) { typeName = typeName.Replace("&", ""); sb.Append("out "); } } else if (@this.ParameterType.IsByRef) { typeName = typeName.Replace("&", ""); sb.Append("ref "); } sb.Append(typeName); } }