History

GSF.Core: Full code review and clean-up related to new GSF.Reflection code additions.

J. Ritchie Carroll committed 10 years ago

Commit 26ad49d1 parent 13cc47611 files · +589 −597Browse files

Showing the changes to Source/Libraries/GSF.Core.Shared/Reflection/MethodBaseExtensions.cs (1 of 11 files). Show all

Unified · Split

Source/Libraries/GSF.Core.Shared/Reflection/MethodBaseExtensions.cs+35−79
@@ -19,7 +19,6 @@ · 18 unmodified lines
1919 // 10/24/2016 - Steven E. Chisholm
2020 // Generated original version of source code.
2121 //
22−//
2322 //******************************************************************************************************
2423
2524 using System;
@@ -29,119 +28,76 @@ using System.Text; · 3 unmodified lines
2928 namespace GSF.Reflection
3029 {
3130 /// <summary>
32− /// Extensions for <see cref="MethodBase"/>.
31+ /// Defines extensions methods related to extension functions for <see cref="MethodBase"/> instances.
3332 /// </summary>
3433 public static class MethodBaseExtensions
3534 {
3635 /// <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&lt;TKey,TValue&gt;(String key, String value)
83− /// Returns String.Empty if none.
36+ /// Gets the friendly method name of the provided type, trimming generic parameters.
8437 /// </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>
8740 public static string GetFriendlyMethodName(this MethodBase method)
8841 {
89− if (method == null)
42+ if ((object)method == null)
9043 return string.Empty;
9144
9245 bool appendComma;
93− var sb = new StringBuilder();
94− sb.Append(method.Name);
46+ StringBuilder name = new StringBuilder();
47+
48+ name.Append(method.Name);
49+
9550 if (method is MethodInfo && method.IsGenericMethod)
9651 {
9752 appendComma = false;
98− sb.Append("<");
99− foreach (var arg in method.GetGenericArguments())
53+ name.Append("<");
54+
55+ foreach (Type arg in method.GetGenericArguments())
10056 {
10157 if (appendComma)
102− {
103− sb.Append(",");
104− }
58+ name.Append(",");
10559 else
106− {
10760 appendComma = true;
108− }
109− sb.Append(arg.Name);
61+
62+ name.Append(arg.Name);
11063 }
111− sb.Append(">");
64+
65+ name.Append(">");
11266 }
67+
11368 appendComma = false;
114− sb.Append("(");
115− foreach (var param in method.GetParameters())
69+ name.Append("(");
70+
71+ foreach (ParameterInfo param in method.GetParameters())
11672 {
11773 if (appendComma)
118− {
119− sb.Append(", ");
120− }
74+ name.Append(", ");
12175 else
122− {
12376 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);
12881 }
129− sb.Append(")");
130− return sb.ToString();
82+
83+ name.Append(")");
84+
85+ return name.ToString();
13186 }
13287
13388 /// <summary>
134− /// Gets a friendly name for the method including class name. Example:
135− /// Namespace.Class`1+Subclass`2.MethodName&lt;TKey,TValue&gt;(String key, String value)
89+ /// Gets the friendly method name with class of the provided type, trimming generic parameters.
13690 /// </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>
13993 public static string GetFriendlyMethodNameWithClass(this MethodBase method)
14094 {
14195 string className = method.GetFriendlyClassName();
14296 string methodName = method.GetFriendlyMethodName();
97+
14398 if (className.Length == 0)
14499 return methodName;
100+
145101 return className + "." + methodName;
146102 }
147103 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t