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