ConstructorInfo - GetSignature
A ConstructorInfo extension method that gets signature.
public static void Main() { ConstructorInfo obj = typeof(GenericTypeConstructorModel<>).GetConstructors()[0]; //C# Extension Method: ConstructorInfo - GetSignature string result = obj.GetSignature(); Console.WriteLine(result); } public class GenericTypeConstructorModel<T> { public GenericTypeConstructorModel(T i) { } }
View Source
using System.Linq; using System.Reflection; using System.Text; public static partial class Extensions { public static string GetSignature(this ConstructorInfo @this) { // Example: [Name] [<GenericArguments] ([Parameters]) var sb = new StringBuilder(); // Name sb.Append(@this.ReflectedType.IsGenericType ? @this.ReflectedType.Name.Substring(0, @this.ReflectedType.Name.IndexOf('`')) : @this.ReflectedType.Name); // Parameters ParameterInfo[] parameters = @this.GetParameters(); sb.Append("("); sb.Append(string.Join(", ", parameters.Select(x => x.GetSignature()))); sb.Append(")"); return sb.ToString(); } }