MethodInfo - GetDeclaraction
A MethodInfo extension method that gets a declaraction.
public static void Main() { MethodInfo StaticMethod = typeof(MethodModel<int>).GetMethod("StaticMethod", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo VirtualMethod = typeof(MethodModel<int>).GetMethod("VirtualMethod", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo ProtectedInternalMethod = typeof(MethodModel<int>).GetMethod("ProtectedInternalMethod", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); //C# Extension Method: MethodInfo - GetDeclaraction string StaticMethodResult = StaticMethod.GetDeclaraction(); string VirtualMethodResult = VirtualMethod.GetDeclaraction(); string ProtectedInternalMethodResult = ProtectedInternalMethod.GetDeclaraction(); Console.WriteLine(StaticMethodResult); Console.WriteLine(VirtualMethodResult); Console.WriteLine(ProtectedInternalMethodResult); } 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.Linq; using System.Reflection; using System.Text; public static partial class Extensions { /// <summary>A MethodInfo 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 MethodInfo @this) { // Example: [Visibility] [Modifier] [Type] [Name] [<GenericArguments] ([Parameters]) var sb = new StringBuilder(); // Visibility if (@this.IsPublic) { sb.Append("public "); } else if (@this.IsFamily) { sb.Append("protected "); } else if (@this.IsAssembly) { sb.Append("internal "); } else if (@this.IsPrivate) { sb.Append("private "); } else { sb.Append("protected internal "); } // Modifier if (@this.IsAbstract) { sb.Append("abstract "); } else if (@this.GetBaseDefinition().DeclaringType != @this.DeclaringType) { sb.Append("override "); } else if (@this.IsVirtual) { sb.Append("virtual "); } else if (@this.IsStatic) { sb.Append("static "); } // Type sb.Append(@this.ReturnType.GetShortDeclaraction()); sb.Append(" "); // Name sb.Append(@this.Name); if (@this.IsGenericMethod) { sb.Append("<"); Type[] arguments = @this.GetGenericArguments(); sb.Append(string.Join(", ", arguments.Select(x => x.GetShortDeclaraction()))); sb.Append(">"); } // Parameters ParameterInfo[] parameters = @this.GetParameters(); sb.Append("("); sb.Append(string.Join(", ", parameters.Select(x => x.GetDeclaraction()))); sb.Append(")"); return sb.ToString(); } }