From cc9eaca87af8e4b628f17a44d12fc4275f63afba Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Thu, 19 Jul 2018 11:38:30 -0400 Subject: [PATCH] feat: Extension method for getting a signature representation of a MethodInfo --- .../Extensions/MethodInfoExtensions.cs | 121 ++++++++++++++++++ .../ICD.Common.Utils_SimplSharp.csproj | 1 + 2 files changed, 122 insertions(+) create mode 100644 ICD.Common.Utils/Extensions/MethodInfoExtensions.cs diff --git a/ICD.Common.Utils/Extensions/MethodInfoExtensions.cs b/ICD.Common.Utils/Extensions/MethodInfoExtensions.cs new file mode 100644 index 0000000..611b38a --- /dev/null +++ b/ICD.Common.Utils/Extensions/MethodInfoExtensions.cs @@ -0,0 +1,121 @@ +using System; +using System.Text; +#if SIMPLSHARP +using Crestron.SimplSharp.Reflection; +#else +using System.Reflection; +#endif + +namespace ICD.Common.Utils.Extensions +{ + public static class MethodInfoExtensions + { + /// + /// Return the method signature as a string. + /// + /// The Method + /// Method signature + public static string GetSignature(this MethodInfo method) + { + return method.GetSignature(false); + } + + /// + /// Return the method signature as a string. + /// + /// The Method + /// Return as a callable string(public void a(string b) would return a(b)) + /// Method signature + public static string GetSignature(this MethodInfo method, bool callable) + { + bool firstParam = true; + StringBuilder sigBuilder = new StringBuilder(); + + if (callable == false) + { + if (method.IsPublic) + sigBuilder.Append("public "); + else if (method.IsPrivate) + sigBuilder.Append("private "); + else if (method.IsAssembly) + sigBuilder.Append("internal "); + if (method.IsFamily) + sigBuilder.Append("protected "); + if (method.IsStatic) + sigBuilder.Append("static "); + + Type returnType = method.ReturnType; + + sigBuilder.Append(returnType.GetSyntaxName()); + sigBuilder.Append(' '); + } + + sigBuilder.Append(method.Name); + + // Add method generics + if (method.IsGenericMethod) + { + sigBuilder.Append("<"); + foreach (Type g in method.GetGenericArguments()) + { + if (firstParam) + firstParam = false; + else + sigBuilder.Append(", "); + + sigBuilder.Append(g.GetSyntaxName()); + } + sigBuilder.Append(">"); + } + + sigBuilder.Append("("); + + firstParam = true; + bool secondParam = false; + + foreach (ParameterInfo param in method.GetParameters()) + { + if (firstParam) + { + firstParam = false; + +#if SIMPLSHARP + // TODO - RestrictionViolationException: System.Runtime.CompilerServices.ExtensionAttribute - Not allowed due to restrictions +#else + if (method.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false)) + { + if (callable) + { + secondParam = true; + continue; + } + sigBuilder.Append("this "); + } +#endif + } + else if (secondParam) + secondParam = false; + else + sigBuilder.Append(", "); + + if (param.ParameterType.IsByRef) + sigBuilder.Append("ref "); + else if (param.GetIsOut()) + sigBuilder.Append("out "); + if (!callable) + { + Type parameterType = param.ParameterType; + + sigBuilder.Append(parameterType.GetSyntaxName()); + sigBuilder.Append(' '); + } + + sigBuilder.Append(param.Name); + } + + sigBuilder.Append(")"); + + return sigBuilder.ToString(); + } + } +} diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index 4984ff7..1e6c200 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -100,6 +100,7 @@ +