Showing the changes to Source/Libraries/GSF.Core.Shared/Reflection/MethodBaseExtensions.cs (1 of 11 files). Show all
Unified · Split
| @@ -19,7 +19,6 @@ · 18 unmodified lines | |||
| 19 | // 10/24/2016 - Steven E. Chisholm | 19 | // 10/24/2016 - Steven E. Chisholm |
| 20 | // Generated original version of source code. | 20 | // Generated original version of source code. |
| 21 | // | 21 | // |
| 22 | // | ||
| 23 | //****************************************************************************************************** | 22 | //****************************************************************************************************** |
| 24 | 23 | ||
| 25 | using System; | 24 | using System; |
| @@ -29,119 +28,76 @@ using System.Text; · 3 unmodified lines | |||
| 29 | namespace GSF.Reflection | 28 | namespace GSF.Reflection |
| 30 | { | 29 | { |
| 31 | /// <summary> | 30 | /// <summary> |
| 32 | /// Extensions for <see cref="MethodBase"/>. | 31 | /// Defines extensions methods related to extension functions for <see cref="MethodBase"/> instances. |
| 33 | /// </summary> | 32 | /// </summary> |
| 34 | public static class MethodBaseExtensions | 33 | public static class MethodBaseExtensions |
| 35 | { | 34 | { |
| 36 | /// <summary> | 35 | /// <summary> |
| 37 | /// Gets the full class name of the provided type. Trimming generic parameters. | 36 | /// Gets the friendly method name of the provided type, trimming generic parameters. |
| 38 | /// Example: System.Collections.Generic.List`1+Enumberable | ||
| 39 | /// Returns String.Empty if none. | ||
| 40 | /// </summary> | ||
| 41 | /// <param name="type"></param> | ||
| 42 | /// <returns></returns> | ||
| 43 | public static string GetFriendlyClassName(this Type type) | ||
| 44 | { | ||
| 45 | if (type != null) | ||
| 46 | { | ||
| 47 | var name = type.FullName; | ||
| 48 | if (name != null) | ||
| 49 | { | ||
| 50 | int length = name.Length; | ||
| 51 | int indexOfBracket = name.IndexOf('['); | ||
| 52 | int indexOfComma = name.IndexOf(','); | ||
| 53 | if (indexOfBracket >= 0) | ||
| 54 | length = Math.Min(indexOfBracket, length); | ||
| 55 | if (indexOfComma >= 0) | ||
| 56 | length = Math.Min(indexOfComma, length); | ||
| 57 | name = name.Substring(0, length).Trim(); | ||
| 58 | return name; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | return string.Empty; | ||
| 62 | } | ||
| 63 | |||
| 64 | /// <summary> | ||
| 65 | /// Gets the full class name of the provided type. Trimming generic parameters. | ||
| 66 | /// Example: System.Collections.Generic.List`1+Enumerable | ||
| 67 | /// Returns String.Empty if none. | ||
| 68 | /// </summary> | ||
| 69 | /// <param name="method"></param> | ||
| 70 | /// <returns></returns> | ||
| 71 | public static string GetFriendlyClassName(this MethodBase method) | ||
| 72 | { | ||
| 73 | if (method != null) | ||
| 74 | { | ||
| 75 | return method.DeclaringType.GetFriendlyClassName(); | ||
| 76 | } | ||
| 77 | return string.Empty; | ||
| 78 | } | ||
| 79 | |||
| 80 | /// <summary> | ||
| 81 | /// Gets a friendly name for the method. Example: | ||
| 82 | /// MethodName<TKey,TValue>(String key, String value) | ||
| 83 | /// Returns String.Empty if none. | ||
| 84 | /// </summary> | 37 | /// </summary> |
| 85 | /// <param name="method"></param> | 38 | /// <param name="method">Type to get friendly method name for.</param> |
| 86 | /// <returns></returns> | 39 | /// <returns>Friendly method name of the provided type, or <see cref="string.Empty"/> if <paramref name="method"/> is <c>null</c>.</returns> |
| 87 | public static string GetFriendlyMethodName(this MethodBase method) | 40 | public static string GetFriendlyMethodName(this MethodBase method) |
| 88 | { | 41 | { |
| 89 | if (method == null) | 42 | if ((object)method == null) |
| 90 | return string.Empty; | 43 | return string.Empty; |
| 91 | 44 | ||
| 92 | bool appendComma; | 45 | bool appendComma; |
| 93 | var sb = new StringBuilder(); | 46 | StringBuilder name = new StringBuilder(); |
| 94 | sb.Append(method.Name); | 47 | |
| 48 | name.Append(method.Name); | ||
| 49 | |||
| 95 | if (method is MethodInfo && method.IsGenericMethod) | 50 | if (method is MethodInfo && method.IsGenericMethod) |
| 96 | { | 51 | { |
| 97 | appendComma = false; | 52 | appendComma = false; |
| 98 | sb.Append("<"); | 53 | name.Append("<"); |
| 99 | foreach (var arg in method.GetGenericArguments()) | 54 | |
| 55 | foreach (Type arg in method.GetGenericArguments()) | ||
| 100 | { | 56 | { |
| 101 | if (appendComma) | 57 | if (appendComma) |
| 102 | { | 58 | name.Append(","); |
| 103 | sb.Append(","); | ||
| 104 | } | ||
| 105 | else | 59 | else |
| 106 | { | ||
| 107 | appendComma = true; | 60 | appendComma = true; |
| 108 | } | 61 | |
| 109 | sb.Append(arg.Name); | 62 | name.Append(arg.Name); |
| 110 | } | 63 | } |
| 111 | sb.Append(">"); | 64 | |
| 65 | name.Append(">"); | ||
| 112 | } | 66 | } |
| 67 | |||
| 113 | appendComma = false; | 68 | appendComma = false; |
| 114 | sb.Append("("); | 69 | name.Append("("); |
| 115 | foreach (var param in method.GetParameters()) | 70 | |
| 71 | foreach (ParameterInfo param in method.GetParameters()) | ||
| 116 | { | 72 | { |
| 117 | if (appendComma) | 73 | if (appendComma) |
| 118 | { | 74 | name.Append(", "); |
| 119 | sb.Append(", "); | ||
| 120 | } | ||
| 121 | else | 75 | else |
| 122 | { | ||
| 123 | appendComma = true; | 76 | appendComma = true; |
| 124 | } | 77 | |
| 125 | sb.Append(param.ParameterType.Name); | 78 | name.Append(param.ParameterType.Name); |
| 126 | sb.Append(" "); | 79 | name.Append(" "); |
| 127 | sb.Append(param.Name); | 80 | name.Append(param.Name); |
| 128 | } | 81 | } |
| 129 | sb.Append(")"); | 82 | |
| 130 | return sb.ToString(); | 83 | name.Append(")"); |
| 84 | |||
| 85 | return name.ToString(); | ||
| 131 | } | 86 | } |
| 132 | 87 | ||
| 133 | /// <summary> | 88 | /// <summary> |
| 134 | /// Gets a friendly name for the method including class name. Example: | 89 | /// Gets the friendly method name with class of the provided type, trimming generic parameters. |
| 135 | /// Namespace.Class`1+Subclass`2.MethodName<TKey,TValue>(String key, String value) | ||
| 136 | /// </summary> | 90 | /// </summary> |
| 137 | /// <param name="method"></param> | 91 | /// <param name="method">Type to get friendly method name with class for.</param> |
| 138 | /// <returns></returns> | 92 | /// <returns>Friendly method name with class of the provided type, or <see cref="string.Empty"/> if <paramref name="method"/> is <c>null</c>.</returns> |
| 139 | public static string GetFriendlyMethodNameWithClass(this MethodBase method) | 93 | public static string GetFriendlyMethodNameWithClass(this MethodBase method) |
| 140 | { | 94 | { |
| 141 | string className = method.GetFriendlyClassName(); | 95 | string className = method.GetFriendlyClassName(); |
| 142 | string methodName = method.GetFriendlyMethodName(); | 96 | string methodName = method.GetFriendlyMethodName(); |
| 97 | |||
| 143 | if (className.Length == 0) | 98 | if (className.Length == 0) |
| 144 | return methodName; | 99 | return methodName; |
| 100 | |||
| 145 | return className + "." + methodName; | 101 | return className + "." + methodName; |
| 146 | } | 102 | } |
| 147 | } | 103 | } |