mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-10 18:24:53 +00:00
feat: Extension method for getting a signature representation of a MethodInfo
This commit is contained in:
121
ICD.Common.Utils/Extensions/MethodInfoExtensions.cs
Normal file
121
ICD.Common.Utils/Extensions/MethodInfoExtensions.cs
Normal file
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Return the method signature as a string.
|
||||
/// </summary>
|
||||
/// <param name="method">The Method</param>
|
||||
/// <returns>Method signature</returns>
|
||||
public static string GetSignature(this MethodInfo method)
|
||||
{
|
||||
return method.GetSignature(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the method signature as a string.
|
||||
/// </summary>
|
||||
/// <param name="method">The Method</param>
|
||||
/// <param name="callable">Return as a callable string(public void a(string b) would return a(b))</param>
|
||||
/// <returns>Method signature</returns>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,6 +100,7 @@
|
||||
<Compile Include="Extensions\DayOfWeekExtensions.cs" />
|
||||
<Compile Include="Extensions\ListExtensions.cs" />
|
||||
<Compile Include="Comparers\PredicateEqualityComparer.cs" />
|
||||
<Compile Include="Extensions\MethodInfoExtensions.cs" />
|
||||
<Compile Include="Extensions\ParameterInfoExtensions.cs" />
|
||||
<Compile Include="Extensions\UriExtensions.cs" />
|
||||
<Compile Include="Extensions\UshortExtensions.cs" />
|
||||
|
||||
Reference in New Issue
Block a user