ParameterInfo - GetDeclaraction

A ParameterInfo extension method that gets a declaraction.

Try it

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 - GetDeclaraction
        Console.WriteLine(p.GetDeclaraction());
    }
}


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 GetDeclaraction(this ParameterInfo @this)
    {
        var sb = new StringBuilder();

        @this.GetDeclaraction(sb);
        return sb.ToString();
    }

    internal static void GetDeclaraction(this ParameterInfo @this, StringBuilder sb)
    {
        // retval attribute
        var attributes = new List<string>();

        string typeName;
        Type elementType = @this.ParameterType.GetElementType();

        if (elementType != null)
        {
            typeName = @this.ParameterType.Name.Replace(elementType.Name, elementType.GetShortDeclaraction());
        }
        else
        {
            typeName = @this.ParameterType.GetShortDeclaraction();
        }


        if (Attribute.IsDefined(@this, typeof (ParamArrayAttribute)))
        {
            sb.Append("params ");
        }
        else if (@this.Position == 0 && @this.Member.IsDefined(typeof (ExtensionAttribute)))
        {
            sb.Append("this ");
        }

        if (@this.IsIn)
        {
            attributes.Add("In");
        }
        if (@this.IsOut)
        {
            if (typeName.Contains("&"))
            {
                typeName = typeName.Replace("&", "");
                sb.Append("out ");
            }
            else
            {
                attributes.Add("Out");
            }
        }
        else if (@this.ParameterType.IsByRef)
        {
            typeName = typeName.Replace("&", "");
            sb.Append("ref ");
        }
        sb.Append(typeName);

        sb.Append(" ");
        sb.Append(@this.Name);

        if (@this.IsOptional)
        {
            if (@this.DefaultValue != Missing.Value)
            {
                sb.Append(" = " + @this.DefaultValue);
            }
            else
            {
                attributes.Add("Optional");
            }
        }


        string attribute = attributes.Count > 0 ? "[" + string.Join(", ", attributes) + "] " : "";
        sb.Insert(0, attribute);
    }
}