| | 1 | | using System.IO; |
| | 2 | | using System.Reflection; |
| | 3 | | using System.Text.RegularExpressions; |
| | 4 | | using System.Xml; |
| | 5 | |
|
| | 6 | | namespace Towel; |
| | 7 | |
|
| | 8 | | /// <summary>Constains static analysis methods of the code (reflection).</summary> |
| | 9 | | public static class Meta |
| | 10 | | { |
| | 11 | | #region Getting Methods Via Reflection |
| | 12 | |
|
| | 13 | | #region System.Type.GetMethod |
| | 14 | |
|
| | 15 | | #if false |
| | 16 | | /// <summary>Gets a method on a type by signature.</summary> |
| | 17 | | /// <typeparam name="Signature"> |
| | 18 | | /// The signature of the method to get as a delegate type. Must match the |
| | 19 | | /// method in return type, parameter types, generic types, parameter names, |
| | 20 | | /// generic parameter names, and delegate/method name. |
| | 21 | | /// </typeparam> |
| | 22 | | /// <param name="declaringType">The declaring type of the method to get.</param> |
| | 23 | | /// <returns>The method info if found. null if not.</returns> |
| | 24 | | public static MethodInfo GetMethod<Signature>(this Type declaringType) |
| | 25 | | where Signature : Delegate |
| | 26 | | { |
| | 27 | | Type signature = typeof(Signature); |
| | 28 | | string name = Regex.Replace(signature.Name, @"`\d+", string.Empty); |
| | 29 | | Type[] signatureGenerics = signature.GetGenericArguments(); |
| | 30 | | MethodInfo signatureMethodInfo = signature.GetMethod("Invoke"); |
| | 31 | | ParameterInfo[] signatureParameters = signatureMethodInfo.GetParameters(); |
| | 32 | | Type signatureGeneric = signature.GetGenericTypeDefinition(); |
| | 33 | | Type[] signatureGenericGenerics = signatureGeneric.GetGenericArguments(); |
| | 34 | | foreach (MethodInfo currentMethodInfo in declaringType.GetMethods( |
| | 35 | | BindingFlags.Instance | |
| | 36 | | BindingFlags.Static | |
| | 37 | | BindingFlags.Public | |
| | 38 | | BindingFlags.NonPublic)) |
| | 39 | | { |
| | 40 | | MethodInfo methodInfo = currentMethodInfo; |
| | 41 | | if (methodInfo.Name != name || |
| | 42 | | signature.IsGenericType != methodInfo.IsGenericMethod) |
| | 43 | | { |
| | 44 | | continue; |
| | 45 | | } |
| | 46 | | if (signature.IsGenericType) |
| | 47 | | { |
| | 48 | | Type[] methodInfoGenerics = methodInfo.GetGenericArguments(); |
| | 49 | | if (methodInfoGenerics.Length != signatureGenerics.Length || |
| | 50 | | !Equate<Type>(signatureGenericGenerics, methodInfoGenerics, (a, b) => a.Name == b.Name)) |
| | 51 | | { |
| | 52 | | continue; |
| | 53 | | } |
| | 54 | | try |
| | 55 | | { |
| | 56 | | methodInfo = methodInfo.MakeGenericMethod(signatureGenerics); |
| | 57 | | } |
| | 58 | | catch (ArgumentException) |
| | 59 | | { |
| | 60 | | // this is likely a contraint validation error |
| | 61 | | continue; |
| | 62 | | } |
| | 63 | | } |
| | 64 | | if (signatureMethodInfo.ReturnType != methodInfo.ReturnType || |
| | 65 | | !Equate<ParameterInfo>(signatureParameters, methodInfo.GetParameters(), (a, b) => a.ParameterType == b.Paramet |
| | 66 | | { |
| | 67 | | continue; |
| | 68 | | } |
| | 69 | | return methodInfo; |
| | 70 | | } |
| | 71 | | return null; |
| | 72 | | } |
| | 73 | | #endif |
| | 74 | |
|
| | 75 | | #endregion |
| | 76 | |
|
| | 77 | | #region GetTryParseMethod |
| | 78 | |
|
| | 79 | | /// <summary>Gets the TryParse <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> TryParse(<see cref= |
| | 80 | | /// <typeparam name="T">The type of the out parameter.</typeparam> |
| | 81 | | /// <returns>The TryParse <see cref="MethodInfo"/> if found or null if not.</returns> |
| 4 | 82 | | public static MethodInfo? GetTryParseMethod<T>() => GetTryParseMethodCache<T>.Value; |
| | 83 | |
|
| | 84 | | /// <summary>Gets the TryParse <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> TryParse(<see cref= |
| | 85 | | /// <param name="a">The type of the out parameter.</param> |
| | 86 | | /// <returns>The TryParse <see cref="MethodInfo"/> if found or null if not.</returns> |
| | 87 | | public static MethodInfo? GetTryParseMethod(Type a) |
| 25 | 88 | | { |
| 25 | 89 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 25 | 90 | | MethodInfo? methodInfo = a.GetMethod("TryParse", |
| 25 | 91 | | BindingFlags.Static | |
| 25 | 92 | | BindingFlags.Public | |
| 25 | 93 | | BindingFlags.NonPublic, |
| 25 | 94 | | null, |
| 25 | 95 | | Ɐ(typeof(string), a.MakeByRefType()), |
| 25 | 96 | | null); |
| 25 | 97 | | return methodInfo is not null && methodInfo.ReturnType == typeof(bool) |
| 25 | 98 | | ? methodInfo |
| 25 | 99 | | : null; |
| 25 | 100 | | } |
| | 101 | |
|
| | 102 | | internal static class GetTryParseMethodCache<T> |
| | 103 | | { |
| 4 | 104 | | internal static readonly MethodInfo? Value = GetTryParseMethod(typeof(T)); |
| | 105 | | } |
| | 106 | |
|
| | 107 | | #endregion |
| | 108 | |
|
| | 109 | | #region GetFactorialMethod |
| | 110 | |
|
| | 111 | | /// <summary>Gets the Factorial <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> Factorial(<typepar |
| | 112 | | /// <typeparam name="T">The type of the out parameter.</typeparam> |
| | 113 | | /// <returns>The IsPrime <see cref="MethodInfo"/> if found or null if not.</returns> |
| 4 | 114 | | public static MethodInfo? GetFactorialMethod<T>() => GetFactorialMethodCache<T>.Value; |
| | 115 | |
|
| | 116 | | /// <summary>Gets the Factorial <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> Factorial(<paramre |
| | 117 | | /// <param name="a">The type of the out parameter.</param> |
| | 118 | | /// <returns>The IsNonNegative <see cref="MethodInfo"/> if found or null if not.</returns> |
| | 119 | | public static MethodInfo? GetFactorialMethod(Type a) |
| 4 | 120 | | { |
| 4 | 121 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 4 | 122 | | MethodInfo? methodInfo = a.GetMethod("Factorial", |
| 4 | 123 | | BindingFlags.Static | |
| 4 | 124 | | BindingFlags.Public | |
| 4 | 125 | | BindingFlags.NonPublic, |
| 4 | 126 | | null, |
| 4 | 127 | | Ɐ(a), |
| 4 | 128 | | null); |
| 4 | 129 | | return methodInfo is not null && methodInfo.ReturnType == typeof(bool) |
| 4 | 130 | | ? methodInfo |
| 4 | 131 | | : null; |
| 4 | 132 | | } |
| | 133 | |
|
| | 134 | | internal static class GetFactorialMethodCache<T> |
| | 135 | | { |
| 4 | 136 | | internal static readonly MethodInfo? Value = GetFactorialMethod(typeof(T)); |
| | 137 | | } |
| | 138 | |
|
| | 139 | | #endregion |
| | 140 | |
|
| | 141 | | #region GetIsPrimeMethod |
| | 142 | |
|
| | 143 | | /// <summary>Gets the IsPrime <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsPrime(<typeparamre |
| | 144 | | /// <typeparam name="T">The type of the out parameter.</typeparam> |
| | 145 | | /// <returns>The IsPrime <see cref="MethodInfo"/> if found or null if not.</returns> |
| 1 | 146 | | public static MethodInfo? GetIsPrimeMethod<T>() => GetIsPrimeMethodCache<T>.Value; |
| | 147 | |
|
| | 148 | | /// <summary>Gets the IsPrime <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsPrime(<paramref na |
| | 149 | | /// <param name="a">The type of the out parameter.</param> |
| | 150 | | /// <returns>The IsNonNegative <see cref="MethodInfo"/> if found or null if not.</returns> |
| | 151 | | public static MethodInfo? GetIsPrimeMethod(Type a) |
| 1 | 152 | | { |
| 1 | 153 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 1 | 154 | | MethodInfo? methodInfo = a.GetMethod("IsPrime", |
| 1 | 155 | | BindingFlags.Static | |
| 1 | 156 | | BindingFlags.Public | |
| 1 | 157 | | BindingFlags.NonPublic, |
| 1 | 158 | | null, |
| 1 | 159 | | Ɐ(a), |
| 1 | 160 | | null); |
| 1 | 161 | | return methodInfo is not null && methodInfo.ReturnType == typeof(bool) |
| 1 | 162 | | ? methodInfo |
| 1 | 163 | | : null; |
| 1 | 164 | | } |
| | 165 | |
|
| | 166 | | internal static class GetIsPrimeMethodCache<T> |
| | 167 | | { |
| 1 | 168 | | internal static readonly MethodInfo? Value = GetIsPrimeMethod(typeof(T)); |
| | 169 | | } |
| | 170 | |
|
| | 171 | | #endregion |
| | 172 | |
|
| | 173 | | #region GetIsNonNegativeMethod |
| | 174 | |
|
| | 175 | | /// <summary>Gets the IsNonNegative <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsNonNegative( |
| | 176 | | /// <typeparam name="T">The type of the out parameter.</typeparam> |
| | 177 | | /// <returns>The IsNonNegative <see cref="MethodInfo"/> if found or null if not.</returns> |
| 0 | 178 | | public static MethodInfo? GetIsNonNegativeMethod<T>() => GetIsNonNegativeMethodCache<T>.Value; |
| | 179 | |
|
| | 180 | | /// <summary>Gets the IsNonNegative <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsNonNegative( |
| | 181 | | /// <param name="a">The type of the out parameter.</param> |
| | 182 | | /// <returns>The IsNonNegative <see cref="MethodInfo"/> if found or null if not.</returns> |
| | 183 | | public static MethodInfo? GetIsNonNegativeMethod(Type a) |
| 0 | 184 | | { |
| 0 | 185 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 0 | 186 | | MethodInfo? methodInfo = a.GetMethod("IsNonNegative", |
| 0 | 187 | | BindingFlags.Static | |
| 0 | 188 | | BindingFlags.Public | |
| 0 | 189 | | BindingFlags.NonPublic, |
| 0 | 190 | | null, |
| 0 | 191 | | Ɐ(a), |
| 0 | 192 | | null); |
| 0 | 193 | | return methodInfo is not null && methodInfo.ReturnType == typeof(bool) |
| 0 | 194 | | ? methodInfo |
| 0 | 195 | | : null; |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | internal static class GetIsNonNegativeMethodCache<T> |
| | 199 | | { |
| 0 | 200 | | internal static readonly MethodInfo? Value = GetIsNonNegativeMethod(typeof(T)); |
| | 201 | | } |
| | 202 | |
|
| | 203 | | #endregion |
| | 204 | |
|
| | 205 | | #region GetIsNegativeMethod |
| | 206 | |
|
| | 207 | | /// <summary>Gets the IsNegative <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsNegative(<typep |
| | 208 | | /// <typeparam name="T">The type of the out parameter.</typeparam> |
| | 209 | | /// <returns>The IsNegative <see cref="MethodInfo"/> if found or null if not.</returns> |
| 4 | 210 | | public static MethodInfo? GetIsNegativeMethod<T>() => GetIsNegativeMethodCache<T>.Value; |
| | 211 | |
|
| | 212 | | /// <summary>Gets the IsNegative <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsNegative(<param |
| | 213 | | /// <param name="a">The type of the out parameter.</param> |
| | 214 | | /// <returns>The IsNegative <see cref="MethodInfo"/> if found or null if not.</returns> |
| | 215 | | public static MethodInfo? GetIsNegativeMethod(Type a) |
| 4 | 216 | | { |
| 4 | 217 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 4 | 218 | | MethodInfo? methodInfo = a.GetMethod("IsNegative", |
| 4 | 219 | | BindingFlags.Static | |
| 4 | 220 | | BindingFlags.Public | |
| 4 | 221 | | BindingFlags.NonPublic, |
| 4 | 222 | | null, |
| 4 | 223 | | Ɐ(a), |
| 4 | 224 | | null); |
| 4 | 225 | | return methodInfo is not null && methodInfo.ReturnType == typeof(bool) |
| 4 | 226 | | ? methodInfo |
| 4 | 227 | | : null; |
| 4 | 228 | | } |
| | 229 | |
|
| | 230 | | internal static class GetIsNegativeMethodCache<T> |
| | 231 | | { |
| 4 | 232 | | internal static readonly MethodInfo? Value = GetIsNegativeMethod(typeof(T)); |
| | 233 | | } |
| | 234 | |
|
| | 235 | | #endregion |
| | 236 | |
|
| | 237 | | #region GetIsPositiveMethod |
| | 238 | |
|
| | 239 | | /// <summary>Gets the IsPositive <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsPositive(<typep |
| | 240 | | /// <typeparam name="T">The type of the out parameter.</typeparam> |
| | 241 | | /// <returns>The IsPositive <see cref="MethodInfo"/> if found or null if not.</returns> |
| 4 | 242 | | public static MethodInfo? GetIsPositiveMethod<T>() => GetIsPositiveMethodCache<T>.Value; |
| | 243 | |
|
| | 244 | | /// <summary>Gets the IsPositive <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsPositive(<param |
| | 245 | | /// <param name="a">The type of the out parameter.</param> |
| | 246 | | /// <returns>The IsPositive <see cref="MethodInfo"/> if found or null if not.</returns> |
| | 247 | | public static MethodInfo? GetIsPositiveMethod(Type a) |
| 4 | 248 | | { |
| 4 | 249 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 4 | 250 | | MethodInfo? methodInfo = a.GetMethod("IsPositive", |
| 4 | 251 | | BindingFlags.Static | |
| 4 | 252 | | BindingFlags.Public | |
| 4 | 253 | | BindingFlags.NonPublic, |
| 4 | 254 | | null, |
| 4 | 255 | | Ɐ(a), |
| 4 | 256 | | null); |
| 4 | 257 | | return methodInfo is not null && methodInfo.ReturnType == typeof(bool) |
| 4 | 258 | | ? methodInfo |
| 4 | 259 | | : null; |
| 4 | 260 | | } |
| | 261 | |
|
| | 262 | | internal static class GetIsPositiveMethodCache<T> |
| | 263 | | { |
| 4 | 264 | | internal static readonly MethodInfo? Value = GetIsPositiveMethod(typeof(T)); |
| | 265 | | } |
| | 266 | |
|
| | 267 | | #endregion |
| | 268 | |
|
| | 269 | | #region GetIsEvenMethod |
| | 270 | |
|
| | 271 | | /// <summary>Gets the IsEven <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsEven(<typeparamref |
| | 272 | | /// <typeparam name="T">The type of the out parameter.</typeparam> |
| | 273 | | /// <returns>The IsEven <see cref="MethodInfo"/> if found or null if not.</returns> |
| 4 | 274 | | public static MethodInfo? GetIsEvenMethod<T>() => GetIsEvenMethodCache<T>.Value; |
| | 275 | |
|
| | 276 | | /// <summary>Gets the IsEven <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsEven(<paramref name |
| | 277 | | /// <param name="a">The type of the out parameter.</param> |
| | 278 | | /// <returns>The IsEven <see cref="MethodInfo"/> if found or null if not.</returns> |
| | 279 | | public static MethodInfo? GetIsEvenMethod(Type a) |
| 4 | 280 | | { |
| 4 | 281 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 4 | 282 | | MethodInfo? methodInfo = a.GetMethod( |
| 4 | 283 | | "IsOdd", |
| 4 | 284 | | BindingFlags.Static | |
| 4 | 285 | | BindingFlags.Public | |
| 4 | 286 | | BindingFlags.NonPublic, |
| 4 | 287 | | null, |
| 4 | 288 | | Ɐ(a), |
| 4 | 289 | | null); |
| 4 | 290 | | return methodInfo is not null && methodInfo.ReturnType == typeof(bool) |
| 4 | 291 | | ? methodInfo |
| 4 | 292 | | : null; |
| 4 | 293 | | } |
| | 294 | |
|
| | 295 | | internal static class GetIsEvenMethodCache<T> |
| | 296 | | { |
| 4 | 297 | | internal static readonly MethodInfo? Value = GetIsEvenMethod(typeof(T)); |
| | 298 | | } |
| | 299 | |
|
| | 300 | | #endregion |
| | 301 | |
|
| | 302 | | #region GetIsOddMethod |
| | 303 | |
|
| | 304 | | /// <summary>Gets the IsOdd <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsOdd(<typeparamref na |
| | 305 | | /// <typeparam name="T">The type of the out parameter.</typeparam> |
| | 306 | | /// <returns>The IsOdd <see cref="MethodInfo"/> if found or null if not.</returns> |
| 8 | 307 | | public static MethodInfo? GetIsOddMethod<T>() => GetIsOddMethodCache<T>.Value; |
| | 308 | |
|
| | 309 | | /// <summary>Gets the IsOdd <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsOdd(<paramref name=" |
| | 310 | | /// <param name="a">The type of the out parameter.</param> |
| | 311 | | /// <returns>The IsOdd <see cref="MethodInfo"/> if found or null if not.</returns> |
| | 312 | | public static MethodInfo? GetIsOddMethod(Type a) |
| 8 | 313 | | { |
| 8 | 314 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 8 | 315 | | MethodInfo? methodInfo = a.GetMethod( |
| 8 | 316 | | "IsOdd", |
| 8 | 317 | | BindingFlags.Static | |
| 8 | 318 | | BindingFlags.Public | |
| 8 | 319 | | BindingFlags.NonPublic, |
| 8 | 320 | | null, |
| 8 | 321 | | Ɐ(a), |
| 8 | 322 | | null); |
| 8 | 323 | | return methodInfo is not null && methodInfo.ReturnType == typeof(bool) |
| 8 | 324 | | ? methodInfo |
| 8 | 325 | | : null; |
| 8 | 326 | | } |
| | 327 | |
|
| | 328 | | internal static class GetIsOddMethodCache<T> |
| | 329 | | { |
| 8 | 330 | | internal static readonly MethodInfo? Value = GetIsOddMethod(typeof(T)); |
| | 331 | | } |
| | 332 | |
|
| | 333 | | #endregion |
| | 334 | |
|
| | 335 | | #region GetIsIntegerMethod |
| | 336 | |
|
| | 337 | | /// <summary>Gets the IsInteger <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsInteger(<typepar |
| | 338 | | /// <typeparam name="T">The type of the out parameter.</typeparam> |
| | 339 | | /// <returns>The TryParse <see cref="MethodInfo"/> if found or null if not.</returns> |
| 4 | 340 | | public static MethodInfo? GetIsIntegerMethod<T>() => GetIsIntegerMethodCache<T>.Value; |
| | 341 | |
|
| | 342 | | /// <summary>Gets the IsInteger <see cref="MethodInfo"/> on a type if it exists [<see cref="bool"/> IsInteger(<paramre |
| | 343 | | /// <param name="a">The type of the out parameter.</param> |
| | 344 | | /// <returns>The TryParse <see cref="MethodInfo"/> if found or null if not.</returns> |
| | 345 | | public static MethodInfo? GetIsIntegerMethod(Type a) |
| 4 | 346 | | { |
| 4 | 347 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 4 | 348 | | MethodInfo? methodInfo = a.GetMethod( |
| 4 | 349 | | "IsInteger", |
| 4 | 350 | | BindingFlags.Static | |
| 4 | 351 | | BindingFlags.Public | |
| 4 | 352 | | BindingFlags.NonPublic, |
| 4 | 353 | | null, |
| 4 | 354 | | Ɐ(a), |
| 4 | 355 | | null); |
| 4 | 356 | | return methodInfo is not null && methodInfo.ReturnType == typeof(bool) |
| 4 | 357 | | ? methodInfo |
| 4 | 358 | | : null; |
| 4 | 359 | | } |
| | 360 | |
|
| | 361 | | internal static class GetIsIntegerMethodCache<T> |
| | 362 | | { |
| 4 | 363 | | internal static readonly MethodInfo? Value = GetIsIntegerMethod(typeof(T)); |
| | 364 | | } |
| | 365 | |
|
| | 366 | | #endregion |
| | 367 | |
|
| | 368 | | #region GetLessThanMethod |
| | 369 | |
|
| | 370 | | /// <summary>Determines if an op_LessThan member exists.</summary> |
| | 371 | | /// <typeparam name="TA">The type of the left operand.</typeparam> |
| | 372 | | /// <typeparam name="TB">The type of the right operand.</typeparam> |
| | 373 | | /// <typeparam name="TC">The type of the return.</typeparam> |
| | 374 | | /// <returns>True if the op_LessThan member exists or false if not.</returns> |
| 0 | 375 | | public static MethodInfo? GetLessThanMethod<TA, TB, TC>() => GetLessThanMethodCache<TA, TB, TC>.Value; |
| | 376 | |
|
| | 377 | | /// <summary>Determines if an op_LessThan member exists.</summary> |
| | 378 | | /// <param name="a">The type of the left operand.</param> |
| | 379 | | /// <param name="b">The type of the right operand.</param> |
| | 380 | | /// <param name="c">The type of the return.</param> |
| | 381 | | /// <returns>True if the op_LessThan member exists or false if not.</returns> |
| | 382 | | internal static MethodInfo? GetLessThanMethod(Type a, Type b, Type c) |
| 0 | 383 | | { |
| 0 | 384 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 0 | 385 | | if (b is null) throw new ArgumentNullException(nameof(b)); |
| | 386 | | MethodInfo? CheckType(Type type) |
| 0 | 387 | | { |
| 0 | 388 | | MethodInfo? methodInfo = type.GetMethod( |
| 0 | 389 | | "op_LessThan", |
| 0 | 390 | | BindingFlags.Static | |
| 0 | 391 | | BindingFlags.Public | |
| 0 | 392 | | BindingFlags.NonPublic, |
| 0 | 393 | | null, |
| 0 | 394 | | Ɐ(a, b), |
| 0 | 395 | | null); |
| 0 | 396 | | return methodInfo is not null |
| 0 | 397 | | && methodInfo.ReturnType == c |
| 0 | 398 | | && methodInfo.IsSpecialName |
| 0 | 399 | | ? methodInfo |
| 0 | 400 | | : null; |
| 0 | 401 | | } |
| 0 | 402 | | return CheckType(a) ?? CheckType(b); |
| 0 | 403 | | } |
| | 404 | |
|
| | 405 | | internal static class GetLessThanMethodCache<TA, TB, TC> |
| | 406 | | { |
| 0 | 407 | | internal static readonly MethodInfo? Value = GetLessThanMethod(typeof(TA), typeof(TB), typeof(TC)); |
| | 408 | | } |
| | 409 | |
|
| | 410 | | #endregion |
| | 411 | |
|
| | 412 | | #region GetGreaterThanMethod |
| | 413 | |
|
| | 414 | | /// <summary>Determines if an op_GreaterThan member exists.</summary> |
| | 415 | | /// <typeparam name="TA">The type of the left operand.</typeparam> |
| | 416 | | /// <typeparam name="TB">The type of the right operand.</typeparam> |
| | 417 | | /// <typeparam name="TC">The type of the return.</typeparam> |
| | 418 | | /// <returns>True if the op_GreaterThan member exists or false if not.</returns> |
| 0 | 419 | | public static MethodInfo? GetGreaterThanMethod<TA, TB, TC>() => GetGreaterThanMethodCache<TA, TB, TC>.Value; |
| | 420 | |
|
| | 421 | | /// <summary>Determines if an op_GreaterThan member exists.</summary> |
| | 422 | | /// <param name="a">The type of the left operand.</param> |
| | 423 | | /// <param name="b">The type of the right operand.</param> |
| | 424 | | /// <param name="c">The type of the return.</param> |
| | 425 | | /// <returns>True if the op_GreaterThan member exists or false if not.</returns> |
| | 426 | | internal static MethodInfo? GetGreaterThanMethod(Type a, Type b, Type c) |
| 0 | 427 | | { |
| 0 | 428 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 0 | 429 | | if (b is null) throw new ArgumentNullException(nameof(b)); |
| | 430 | | MethodInfo? CheckType(Type type) |
| 0 | 431 | | { |
| 0 | 432 | | MethodInfo? methodInfo = type.GetMethod( |
| 0 | 433 | | "op_GreaterThan", |
| 0 | 434 | | BindingFlags.Static | |
| 0 | 435 | | BindingFlags.Public | |
| 0 | 436 | | BindingFlags.NonPublic, |
| 0 | 437 | | null, |
| 0 | 438 | | Ɐ(a, b), |
| 0 | 439 | | null); |
| 0 | 440 | | return methodInfo is not null |
| 0 | 441 | | && methodInfo.ReturnType == c |
| 0 | 442 | | && methodInfo.IsSpecialName |
| 0 | 443 | | ? methodInfo |
| 0 | 444 | | : null; |
| 0 | 445 | | } |
| 0 | 446 | | return CheckType(a) ?? CheckType(b); |
| 0 | 447 | | } |
| | 448 | |
|
| | 449 | | internal static class GetGreaterThanMethodCache<TA, TB, TC> |
| | 450 | | { |
| 0 | 451 | | internal static readonly MethodInfo? Value = GetGreaterThanMethod(typeof(TA), typeof(TB), typeof(TC)); |
| | 452 | | } |
| | 453 | |
|
| | 454 | | #endregion |
| | 455 | |
|
| | 456 | | #endregion |
| | 457 | |
|
| | 458 | | #region Has[Implicit|Explicit]Cast |
| | 459 | |
|
| | 460 | | /// <summary>Determines if an implicit casting operator exists from one type to another.</summary> |
| | 461 | | /// <typeparam name="TFrom">The parameter type of the implicit casting operator.</typeparam> |
| | 462 | | /// <typeparam name="TTo">The return type fo the implicit casting operator.</typeparam> |
| | 463 | | /// <returns>True if the implicit casting operator exists or false if not.</returns> |
| 0 | 464 | | public static bool HasImplicitCast<TFrom, TTo>() => HasCastCache<TFrom, TTo>.Implicit; |
| | 465 | |
|
| | 466 | | /// <summary>Determines if an implicit casting operator exists from one type to another.</summary> |
| | 467 | | /// <typeparam name="TFrom">The parameter type of the implicit casting operator.</typeparam> |
| | 468 | | /// <typeparam name="TTo">The return type fo the implicit casting operator.</typeparam> |
| | 469 | | /// <returns>True if the implicit casting operator exists or false if not.</returns> |
| 0 | 470 | | public static bool HasExplicitCast<TFrom, TTo>() => HasCastCache<TFrom, TTo>.Implicit; |
| | 471 | |
|
| | 472 | | /// <summary>Determines if an implicit casting operator exists from one type to another.</summary> |
| | 473 | | /// <param name="fromType">The parameter type of the implicit casting operator.</param> |
| | 474 | | /// <param name="toType">The return type fo the implicit casting operator.</param> |
| | 475 | | /// <returns>True if the implicit casting operator exists or false if not.</returns> |
| 0 | 476 | | public static bool HasImplicitCast(Type fromType, Type toType) => HasCast(fromType, toType, true); |
| | 477 | |
|
| | 478 | | /// <summary>Determines if an implicit casting operator exists from one type to another.</summary> |
| | 479 | | /// <param name="fromType">The parameter type of the implicit casting operator.</param> |
| | 480 | | /// <param name="toType">The return type fo the implicit casting operator.</param> |
| | 481 | | /// <returns>True if the implicit casting operator exists or false if not.</returns> |
| 0 | 482 | | public static bool HasExplicitCast(Type fromType, Type toType) => HasCast(fromType, toType, false); |
| | 483 | |
|
| | 484 | | internal static bool HasCast(Type fromType, Type toType, bool @implicit) |
| 0 | 485 | | { |
| 0 | 486 | | if (fromType is null) throw new ArgumentNullException(nameof(fromType)); |
| 0 | 487 | | if (toType is null) throw new ArgumentNullException(nameof(toType)); |
| 0 | 488 | | string methodName = @implicit |
| 0 | 489 | | ? "op_Implicit" |
| 0 | 490 | | : "op_Explicit"; |
| | 491 | | bool CheckType(Type type) |
| 0 | 492 | | { |
| 0 | 493 | | MethodInfo? methodInfo = type.GetMethod( |
| 0 | 494 | | methodName, |
| 0 | 495 | | BindingFlags.Static | |
| 0 | 496 | | BindingFlags.Public | |
| 0 | 497 | | BindingFlags.NonPublic, |
| 0 | 498 | | null, |
| 0 | 499 | | Ɐ(fromType), |
| 0 | 500 | | null); |
| 0 | 501 | | return methodInfo is not null |
| 0 | 502 | | && methodInfo.ReturnType == toType |
| 0 | 503 | | && methodInfo.IsSpecialName; |
| 0 | 504 | | } |
| 0 | 505 | | if (CheckType(fromType) || CheckType(toType)) |
| 0 | 506 | | { |
| 0 | 507 | | return true; |
| | 508 | | } |
| 0 | 509 | | return false; |
| 0 | 510 | | } |
| | 511 | |
|
| | 512 | | internal static class HasCastCache<TFrom, TTo> |
| | 513 | | { |
| 0 | 514 | | internal static readonly bool Implicit = HasCast(typeof(TFrom), typeof(TTo), true); |
| 0 | 515 | | internal static readonly bool Explicit = HasCast(typeof(TFrom), typeof(TTo), false); |
| | 516 | | } |
| | 517 | |
|
| | 518 | | #endregion |
| | 519 | |
|
| | 520 | | #region System.Type.ConvertToCSharpSource |
| | 521 | |
|
| | 522 | | /// <summary>Converts a <see cref="System.Type"/> into a <see cref="string"/> as it would appear in C# source code.</s |
| | 523 | | /// <param name="type">The <see cref="System.Type"/> to convert to a <see cref="string"/>.</param> |
| | 524 | | /// <param name="showGenericParameters">If the generic parameters are the generic types, whether they should be shown |
| | 525 | | /// <returns>The <see cref="string"/> as the <see cref="System.Type"/> would appear in C# source code.</returns> |
| | 526 | | public static string ConvertToCSharpSource(this Type type, bool showGenericParameters = false) |
| 77 | 527 | | { |
| 77 | 528 | | IQueue<Type> genericParameters = new QueueArray<Type>(); |
| 135 | 529 | | type.GetGenericArguments().Stepper(x => genericParameters.Enqueue(x)); |
| 77 | 530 | | return ConvertToCsharpSource(type); |
| | 531 | |
|
| | 532 | | string ConvertToCsharpSource(Type type) |
| 90 | 533 | | { |
| 90 | 534 | | if (type is null) throw new ArgumentNullException(nameof(type)); |
| 90 | 535 | | string result = type.IsNested |
| 90 | 536 | | ? ConvertToCsharpSource(sourceof(type.DeclaringType, out string c1) ?? throw new ArgumentException(c1)) + "." |
| 90 | 537 | | : type.Namespace + "."; |
| 90 | 538 | | result += Regex.Replace(type.Name, "`.*", string.Empty); |
| 90 | 539 | | if (type.IsGenericType) |
| 30 | 540 | | { |
| 30 | 541 | | result += "<"; |
| 30 | 542 | | bool firstIteration = true; |
| 210 | 543 | | foreach (Type generic in type.GetGenericArguments()) |
| 62 | 544 | | { |
| 62 | 545 | | if (genericParameters.Count <= 0) |
| 4 | 546 | | { |
| 4 | 547 | | break; |
| | 548 | | } |
| 58 | 549 | | Type correctGeneric = genericParameters.Dequeue(); |
| 58 | 550 | | result += (firstIteration ? string.Empty : ",") + |
| 58 | 551 | | (correctGeneric.IsGenericParameter |
| 58 | 552 | | ? (showGenericParameters ? (firstIteration ? string.Empty : " ") + correctGeneric.Name : string.Empty) |
| 58 | 553 | | : (firstIteration ? string.Empty : " ") + ConvertToCSharpSource(correctGeneric, showGenericParameters)); |
| 58 | 554 | | firstIteration = false; |
| 58 | 555 | | } |
| 30 | 556 | | result += ">"; |
| 30 | 557 | | } |
| 90 | 558 | | return result; |
| 90 | 559 | | } |
| 77 | 560 | | } |
| | 561 | |
|
| | 562 | | #endregion |
| | 563 | |
|
| | 564 | | #region System.Enum |
| | 565 | |
|
| | 566 | | /// <summary>Gets a custom attribute on an enum value by generic type.</summary> |
| | 567 | | /// <typeparam name="TAttribute">The type of attribute to get.</typeparam> |
| | 568 | | /// <param name="enum">The enum value to get the attribute of.</param> |
| | 569 | | /// <returns>The attribute on the enum value of the provided type.</returns> |
| | 570 | | public static TAttribute? GetEnumAttribute<TAttribute>(this Enum @enum) |
| | 571 | | where TAttribute : Attribute |
| 296 | 572 | | { |
| 296 | 573 | | Type type = @enum.GetType(); |
| 296 | 574 | | MemberInfo memberInfo = type.GetMember(@enum.ToString())[0]; |
| 296 | 575 | | return memberInfo.GetCustomAttribute<TAttribute>(); |
| 296 | 576 | | } |
| | 577 | |
|
| | 578 | | /// <summary>Gets custom attributes on an enum value by generic type.</summary> |
| | 579 | | /// <typeparam name="TAttribute">The type of attribute to get.</typeparam> |
| | 580 | | /// <param name="enum">The enum value to get the attribute of.</param> |
| | 581 | | /// <returns>The attributes on the enum value of the provided type.</returns> |
| | 582 | | public static System.Collections.Generic.IEnumerable<TAttribute> GetEnumAttributes<TAttribute>(this Enum @enum) |
| | 583 | | where TAttribute : Attribute |
| 122 | 584 | | { |
| 122 | 585 | | Type type = @enum.GetType(); |
| 122 | 586 | | MemberInfo memberInfo = type.GetMember(@enum.ToString())[0]; |
| 122 | 587 | | return memberInfo.GetCustomAttributes<TAttribute>(); |
| 122 | 588 | | } |
| | 589 | |
|
| | 590 | | /// <summary>Gets the maximum value of an enum.</summary> |
| | 591 | | /// <typeparam name="TEnum">The enum type to get the maximum value of.</typeparam> |
| | 592 | | /// <returns>The maximum enum value of the provided type.</returns> |
| | 593 | | public static TEnum GetLastEnumValue<TEnum>() |
| | 594 | | where TEnum : struct, Enum |
| 7 | 595 | | { |
| 7 | 596 | | TEnum[] values = (TEnum[])Enum.GetValues(typeof(TEnum)); |
| 7 | 597 | | if (values.Length is 0) |
| 0 | 598 | | { |
| 0 | 599 | | throw new InvalidOperationException("Attempting to get the last enum value of an enum type with no values."); |
| | 600 | | } |
| 7 | 601 | | return values[^1]; |
| 7 | 602 | | } |
| | 603 | |
|
| | 604 | | #endregion |
| | 605 | |
|
| | 606 | | #region System.Reflection.Assembly |
| | 607 | |
|
| | 608 | | /// <summary>Enumerates through all the events with a custom attribute.</summary> |
| | 609 | | /// <typeparam name="TAttribute">The type of the custom attribute.</typeparam> |
| | 610 | | /// <param name="assembly">The assembly to iterate through the events of.</param> |
| | 611 | | /// <returns>The IEnumerable of the events with the provided attribute type.</returns> |
| | 612 | | public static System.Collections.Generic.IEnumerable<EventInfo> GetEventInfosWithAttribute<TAttribute>(this Assembly a |
| | 613 | | where TAttribute : Attribute |
| 1 | 614 | | { |
| 693 | 615 | | foreach (Type type in assembly.GetTypes()) |
| 345 | 616 | | { |
| 1057 | 617 | | foreach (EventInfo eventInfo in type.GetEvents( |
| 345 | 618 | | BindingFlags.Instance | |
| 345 | 619 | | BindingFlags.Static | |
| 345 | 620 | | BindingFlags.Public | |
| 345 | 621 | | BindingFlags.NonPublic)) |
| 11 | 622 | | { |
| 11 | 623 | | if (eventInfo.GetCustomAttributes(typeof(TAttribute), true).Length > 0) |
| 9 | 624 | | { |
| 9 | 625 | | yield return eventInfo; |
| 9 | 626 | | } |
| 11 | 627 | | } |
| 345 | 628 | | } |
| 1 | 629 | | } |
| | 630 | |
|
| | 631 | | /// <summary>Enumerates through all the constructors with a custom attribute.</summary> |
| | 632 | | /// <typeparam name="TAttribute">The type of the custom attribute.</typeparam> |
| | 633 | | /// <param name="assembly">The assembly to iterate through the constructors of.</param> |
| | 634 | | /// <returns>The IEnumerable of the constructors with the provided attribute type.</returns> |
| | 635 | | public static System.Collections.Generic.IEnumerable<ConstructorInfo> GetConstructorInfosWithAttribute<TAttribute>(thi |
| | 636 | | where TAttribute : Attribute |
| 1 | 637 | | { |
| 693 | 638 | | foreach (Type type in assembly.GetTypes()) |
| 345 | 639 | | { |
| 1683 | 640 | | foreach (ConstructorInfo constructorInfo in type.GetConstructors( |
| 345 | 641 | | BindingFlags.Instance | |
| 345 | 642 | | BindingFlags.Public | |
| 345 | 643 | | BindingFlags.NonPublic)) |
| 324 | 644 | | { |
| 324 | 645 | | if (constructorInfo.GetCustomAttributes(typeof(TAttribute), true).Length > 0) |
| 14 | 646 | | { |
| 14 | 647 | | yield return constructorInfo; |
| 14 | 648 | | } |
| 324 | 649 | | } |
| 345 | 650 | | } |
| 1 | 651 | | } |
| | 652 | |
|
| | 653 | | /// <summary>Enumerates through all the properties with a custom attribute.</summary> |
| | 654 | | /// <typeparam name="TAttribute">The type of the custom attribute.</typeparam> |
| | 655 | | /// <param name="assembly">The assembly to iterate through the properties of.</param> |
| | 656 | | /// <returns>The IEnumerable of the properties with the provided attribute type.</returns> |
| | 657 | | public static System.Collections.Generic.IEnumerable<PropertyInfo> GetPropertyInfosWithAttribute<TAttribute>(this Asse |
| | 658 | | where TAttribute : Attribute |
| 1 | 659 | | { |
| 693 | 660 | | foreach (Type type in assembly.GetTypes()) |
| 345 | 661 | | { |
| 1131 | 662 | | foreach (PropertyInfo propertyInfo in type.GetProperties( |
| 345 | 663 | | BindingFlags.Instance | |
| 345 | 664 | | BindingFlags.Static | |
| 345 | 665 | | BindingFlags.Public | |
| 345 | 666 | | BindingFlags.NonPublic)) |
| 48 | 667 | | { |
| 48 | 668 | | if (propertyInfo.GetCustomAttributes(typeof(TAttribute), true).Length > 0) |
| 18 | 669 | | { |
| 18 | 670 | | yield return propertyInfo; |
| 18 | 671 | | } |
| 48 | 672 | | } |
| 345 | 673 | | } |
| 1 | 674 | | } |
| | 675 | |
|
| | 676 | | /// <summary>Enumerates through all the fields with a custom attribute.</summary> |
| | 677 | | /// <typeparam name="TAttribute">The type of the custom attribute.</typeparam> |
| | 678 | | /// <param name="assembly">The assembly to iterate through the fields of.</param> |
| | 679 | | /// <returns>The IEnumerable of the fields with the provided attribute type.</returns> |
| | 680 | | public static System.Collections.Generic.IEnumerable<FieldInfo> GetFieldInfosWithAttribute<TAttribute>(this Assembly a |
| | 681 | | where TAttribute : Attribute |
| 1 | 682 | | { |
| 693 | 683 | | foreach (Type type in assembly.GetTypes()) |
| 345 | 684 | | { |
| 3095 | 685 | | foreach (FieldInfo fieldInfo in type.GetFields( |
| 345 | 686 | | BindingFlags.Instance | |
| 345 | 687 | | BindingFlags.Static | |
| 345 | 688 | | BindingFlags.Public | |
| 345 | 689 | | BindingFlags.NonPublic)) |
| 1030 | 690 | | { |
| 1030 | 691 | | if (fieldInfo.GetCustomAttributes(typeof(TAttribute), true).Length > 0) |
| 19 | 692 | | { |
| 19 | 693 | | yield return fieldInfo; |
| 19 | 694 | | } |
| 1030 | 695 | | } |
| 345 | 696 | | } |
| 1 | 697 | | } |
| | 698 | |
|
| | 699 | | /// <summary>Enumerates through all the methods with a custom attribute.</summary> |
| | 700 | | /// <typeparam name="TAttribute">The type of the custom attribute.</typeparam> |
| | 701 | | /// <param name="assembly">The assembly to iterate through the methods of.</param> |
| | 702 | | /// <returns>The IEnumerable of the methods with the provided attribute type.</returns> |
| | 703 | | public static System.Collections.Generic.IEnumerable<MethodInfo> GetMethodInfosWithAttribute<TAttribute>(this Assembly |
| | 704 | | where TAttribute : Attribute |
| 25 | 705 | | { |
| 17325 | 706 | | foreach (Type type in assembly.GetTypes()) |
| 8625 | 707 | | { |
| 201775 | 708 | | foreach (MethodInfo methodInfo in type.GetMethods( |
| 8625 | 709 | | BindingFlags.Instance | |
| 8625 | 710 | | BindingFlags.Static | |
| 8625 | 711 | | BindingFlags.Public | |
| 8625 | 712 | | BindingFlags.NonPublic)) |
| 87950 | 713 | | { |
| 87950 | 714 | | if (methodInfo.GetCustomAttributes(typeof(TAttribute), true).Length > 0) |
| 183 | 715 | | { |
| 183 | 716 | | yield return methodInfo; |
| 183 | 717 | | } |
| 87950 | 718 | | } |
| 8625 | 719 | | } |
| 25 | 720 | | } |
| | 721 | |
|
| | 722 | | /// <summary>Enumerates through all the types with a custom attribute.</summary> |
| | 723 | | /// <typeparam name="TAttribute">The type of the custom attribute.</typeparam> |
| | 724 | | /// <param name="assembly">The assembly to iterate through the types of.</param> |
| | 725 | | /// <returns>The IEnumerable of the types with the provided attribute type.</returns> |
| | 726 | | public static System.Collections.Generic.IEnumerable<Type> GetTypesWithAttribute<TAttribute>(this Assembly assembly) |
| | 727 | | where TAttribute : Attribute |
| 1 | 728 | | { |
| 693 | 729 | | foreach (Type type in assembly.GetTypes()) |
| 345 | 730 | | { |
| 345 | 731 | | if (type.GetCustomAttributes(typeof(TAttribute), true).Length > 0) |
| 20 | 732 | | { |
| 20 | 733 | | yield return type; |
| 20 | 734 | | } |
| 345 | 735 | | } |
| 1 | 736 | | } |
| | 737 | |
|
| | 738 | | /// <summary>Gets all the types in an assembly that derive from a base.</summary> |
| | 739 | | /// <typeparam name="TBase">The base type to get the deriving types of.</typeparam> |
| | 740 | | /// <param name="assembly">The assmebly to perform the search on.</param> |
| | 741 | | /// <returns>The IEnumerable of the types that derive from the provided base.</returns> |
| | 742 | | public static System.Collections.Generic.IEnumerable<Type> GetDerivedTypes<TBase>(this Assembly assembly) |
| 5 | 743 | | { |
| 5 | 744 | | Type @base = typeof(TBase); |
| 5 | 745 | | return assembly.GetTypes().Where(type => |
| 4880 | 746 | | type != @base && |
| 4880 | 747 | | @base.IsAssignableFrom(type)); |
| 5 | 748 | | } |
| | 749 | |
|
| | 750 | | /// <summary>Gets the file path of an assembly.</summary> |
| | 751 | | /// <param name="assembly">The assembly to get the file path of.</param> |
| | 752 | | /// <returns>The file path of the assembly.</returns> |
| | 753 | | public static string? GetDirectoryPath(this Assembly assembly) |
| 8 | 754 | | { |
| 8 | 755 | | string? directoryPath = Path.GetDirectoryName(assembly.Location); |
| 8 | 756 | | return directoryPath == string.Empty ? null : directoryPath; |
| 8 | 757 | | } |
| | 758 | |
|
| | 759 | | #endregion |
| | 760 | |
|
| | 761 | | #region GetXmlName |
| | 762 | |
|
| | 763 | | /// <summary>Gets the XML name of an <see cref="Type"/> as it appears in the XML docs.</summary> |
| | 764 | | /// <param name="type">The field to get the XML name of.</param> |
| | 765 | | /// <returns>The XML name of <paramref name="type"/> as it appears in the XML docs.</returns> |
| | 766 | | public static string GetXmlName(this Type type) |
| 40 | 767 | | { |
| 40 | 768 | | if (type is null) throw new ArgumentNullException(nameof(type)); |
| 40 | 769 | | if (sourceof(type.FullName is null, out string c1)) throw new ArgumentException(c1, nameof(type)); |
| 40 | 770 | | LoadXmlDocumentation(type.Assembly); |
| 40 | 771 | | return "T:" + GetXmlNameTypeSegment(type.FullName!); |
| 40 | 772 | | } |
| | 773 | |
|
| | 774 | | /// <summary>Gets the XML name of an <see cref="MethodInfo"/> as it appears in the XML docs.</summary> |
| | 775 | | /// <param name="methodInfo">The field to get the XML name of.</param> |
| | 776 | | /// <returns>The XML name of <paramref name="methodInfo"/> as it appears in the XML docs.</returns> |
| | 777 | | public static string GetXmlName(this MethodInfo methodInfo) |
| 107 | 778 | | { |
| 107 | 779 | | if (methodInfo is null) throw new ArgumentNullException(nameof(methodInfo)); |
| 107 | 780 | | if (sourceof(methodInfo.DeclaringType is null, out string c1)) throw new ArgumentException(c1, nameof(methodInfo)); |
| 107 | 781 | | return GetXmlNameMethodBase(methodInfo: methodInfo); |
| 107 | 782 | | } |
| | 783 | |
|
| | 784 | | /// <summary>Gets the XML name of an <see cref="ConstructorInfo"/> as it appears in the XML docs.</summary> |
| | 785 | | /// <param name="constructorInfo">The field to get the XML name of.</param> |
| | 786 | | /// <returns>The XML name of <paramref name="constructorInfo"/> as it appears in the XML docs.</returns> |
| | 787 | | public static string GetXmlName(this ConstructorInfo constructorInfo) |
| 29 | 788 | | { |
| 29 | 789 | | if (constructorInfo is null) throw new ArgumentNullException(nameof(constructorInfo)); |
| 29 | 790 | | if (sourceof(constructorInfo.DeclaringType is null, out string c1)) throw new ArgumentException(c1, nameof(construct |
| 29 | 791 | | return GetXmlNameMethodBase(constructorInfo: constructorInfo); |
| 29 | 792 | | } |
| | 793 | |
|
| | 794 | | /// <summary>Gets the XML name of an <see cref="PropertyInfo"/> as it appears in the XML docs.</summary> |
| | 795 | | /// <param name="propertyInfo">The field to get the XML name of.</param> |
| | 796 | | /// <returns>The XML name of <paramref name="propertyInfo"/> as it appears in the XML docs.</returns> |
| | 797 | | public static string GetXmlName(this PropertyInfo propertyInfo) |
| 40 | 798 | | { |
| 40 | 799 | | if (propertyInfo is null) throw new ArgumentNullException(nameof(propertyInfo)); |
| 40 | 800 | | if (sourceof(propertyInfo.DeclaringType is null, out string c1)) throw new ArgumentException(c1, nameof(propertyInfo |
| 40 | 801 | | if (sourceof(propertyInfo.DeclaringType!.FullName is null, out string c2)) throw new ArgumentException(c2, nameof(pr |
| 40 | 802 | | return "P:" + GetXmlNameTypeSegment(propertyInfo.DeclaringType.FullName!) + "." + propertyInfo.Name; |
| 40 | 803 | | } |
| | 804 | |
|
| | 805 | | /// <summary>Gets the XML name of an <see cref="FieldInfo"/> as it appears in the XML docs.</summary> |
| | 806 | | /// <param name="fieldInfo">The field to get the XML name of.</param> |
| | 807 | | /// <returns>The XML name of <paramref name="fieldInfo"/> as it appears in the XML docs.</returns> |
| | 808 | | public static string GetXmlName(this FieldInfo fieldInfo) |
| 42 | 809 | | { |
| 42 | 810 | | if (fieldInfo is null) throw new ArgumentNullException(nameof(fieldInfo)); |
| 42 | 811 | | if (sourceof(fieldInfo.DeclaringType is null, out string c1)) throw new ArgumentException(c1, nameof(fieldInfo)); |
| 42 | 812 | | if (sourceof(fieldInfo.DeclaringType!.FullName is null, out string c2)) throw new ArgumentException(c2, nameof(field |
| 42 | 813 | | return "F:" + GetXmlNameTypeSegment(fieldInfo.DeclaringType.FullName!) + "." + fieldInfo.Name; |
| 42 | 814 | | } |
| | 815 | |
|
| | 816 | | /// <summary>Gets the XML name of an <see cref="EventInfo"/> as it appears in the XML docs.</summary> |
| | 817 | | /// <param name="eventInfo">The event to get the XML name of.</param> |
| | 818 | | /// <returns>The XML name of <paramref name="eventInfo"/> as it appears in the XML docs.</returns> |
| | 819 | | public static string GetXmlName(this EventInfo eventInfo) |
| 18 | 820 | | { |
| 18 | 821 | | if (eventInfo is null) throw new ArgumentNullException(nameof(eventInfo)); |
| 18 | 822 | | if (sourceof(eventInfo.DeclaringType is null, out string c1)) throw new ArgumentException(c1, nameof(eventInfo)); |
| 18 | 823 | | if (sourceof(eventInfo.DeclaringType!.FullName is null, out string c2)) throw new ArgumentException(c2, nameof(event |
| 18 | 824 | | return "E:" + GetXmlNameTypeSegment(eventInfo.DeclaringType.FullName!) + "." + eventInfo.Name; |
| 18 | 825 | | } |
| | 826 | |
|
| | 827 | | internal static string GetXmlNameMethodBase(MethodInfo? methodInfo = null, ConstructorInfo? constructorInfo = null) |
| 136 | 828 | | { |
| 136 | 829 | | if (methodInfo is not null && constructorInfo is not null) |
| 0 | 830 | | { |
| 0 | 831 | | throw new TowelBugException($"{nameof(GetDocumentation)} {nameof(methodInfo)} is not null && {nameof(constructorIn |
| | 832 | | } |
| | 833 | |
|
| 136 | 834 | | if (methodInfo is not null) |
| 107 | 835 | | { |
| 107 | 836 | | if (methodInfo.DeclaringType is null) |
| 0 | 837 | | { |
| 0 | 838 | | throw new ArgumentException($"{nameof(methodInfo)}.{nameof(Type.DeclaringType)} is null"); |
| | 839 | | } |
| 107 | 840 | | else if (methodInfo.DeclaringType.IsGenericType) |
| 40 | 841 | | { |
| 40 | 842 | | methodInfo = methodInfo.DeclaringType.GetGenericTypeDefinition().GetMethods( |
| 40 | 843 | | BindingFlags.Static | |
| 40 | 844 | | BindingFlags.Public | |
| 40 | 845 | | BindingFlags.Instance | |
| 156 | 846 | | BindingFlags.NonPublic).First(x => x.MetadataToken == methodInfo.MetadataToken); |
| 40 | 847 | | } |
| 107 | 848 | | } |
| | 849 | |
|
| 136 | 850 | | MethodBase? methodBase = methodInfo ?? (MethodBase?)constructorInfo; |
| 136 | 851 | | if (sourceof(methodBase is null, out string c1)) throw new TowelBugException(c1); |
| 136 | 852 | | if (sourceof(methodBase!.DeclaringType is null, out string c2)) throw new ArgumentException(c2); |
| | 853 | |
|
| 136 | 854 | | LoadXmlDocumentation(methodBase.DeclaringType!.Assembly); |
| | 855 | |
|
| 136 | 856 | | MapHashLinked<int, string, StringEquate, StringHash> typeGenericMap = new(); |
| 136 | 857 | | Type[] typeGenericArguments = methodBase.DeclaringType.GetGenericArguments(); |
| 474 | 858 | | for (int i = 0; i < typeGenericArguments.Length; i++) |
| 101 | 859 | | { |
| 101 | 860 | | Type typeGeneric = typeGenericArguments[i]; |
| 101 | 861 | | typeGenericMap[typeGeneric.Name] = i; |
| 101 | 862 | | } |
| | 863 | |
|
| 136 | 864 | | MapHashLinked<int, string, StringEquate, StringHash> methodGenericMap = new(); |
| 136 | 865 | | if (constructorInfo is null) |
| 107 | 866 | | { |
| 107 | 867 | | Type[] methodGenericArguments = methodBase.GetGenericArguments(); |
| 310 | 868 | | for (int i = 0; i < methodGenericArguments.Length; i++) |
| 48 | 869 | | { |
| 48 | 870 | | Type methodGeneric = methodGenericArguments[i]; |
| 48 | 871 | | methodGenericMap[methodGeneric.Name] = i; |
| 48 | 872 | | } |
| 107 | 873 | | } |
| | 874 | |
|
| 136 | 875 | | ParameterInfo[] parameterInfos = methodBase.GetParameters(); |
| | 876 | |
|
| 136 | 877 | | string memberTypePrefix = "M:"; |
| 136 | 878 | | string declarationTypeString = GetXmlDocumenationFormattedString(methodBase.DeclaringType, false, typeGenericMap, me |
| 136 | 879 | | string memberNameString = |
| 136 | 880 | | constructorInfo is not null ? "#ctor" : |
| 136 | 881 | | methodBase.Name; |
| 136 | 882 | | string methodGenericArgumentsString = |
| 136 | 883 | | methodGenericMap.Count > 0 ? |
| 136 | 884 | | "``" + methodGenericMap.Count : |
| 136 | 885 | | string.Empty; |
| 136 | 886 | | string parametersString = |
| 136 | 887 | | parameterInfos.Length > 0 ? |
| 196 | 888 | | "(" + string.Join(",", methodBase.GetParameters().Select(x => GetXmlDocumenationFormattedString(x.ParameterType, t |
| 136 | 889 | | string.Empty; |
| | 890 | |
|
| 136 | 891 | | string key = |
| 136 | 892 | | memberTypePrefix + |
| 136 | 893 | | declarationTypeString + |
| 136 | 894 | | "." + |
| 136 | 895 | | memberNameString + |
| 136 | 896 | | methodGenericArgumentsString + |
| 136 | 897 | | parametersString; |
| | 898 | |
|
| 136 | 899 | | if (methodInfo is not null && |
| 136 | 900 | | (methodBase.Name is "op_Implicit" || |
| 136 | 901 | | methodBase.Name is "op_Explicit")) |
| 16 | 902 | | { |
| 16 | 903 | | key += "~" + GetXmlDocumenationFormattedString(methodInfo.ReturnType, true, typeGenericMap, methodGenericMap); |
| 16 | 904 | | } |
| 136 | 905 | | return key; |
| 136 | 906 | | } |
| | 907 | |
|
| | 908 | | internal static string GetXmlDocumenationFormattedString( |
| | 909 | | Type type, |
| | 910 | | bool isMethodParameter, |
| | 911 | | MapHashLinked<int, string, StringEquate, StringHash> typeGenericMap, |
| | 912 | | MapHashLinked<int, string, StringEquate, StringHash> methodGenericMap) |
| 537 | 913 | | { |
| 537 | 914 | | if (type.IsGenericParameter) |
| 138 | 915 | | { |
| 138 | 916 | | var (success, exception, methodIndex) = methodGenericMap.TryGet(type.Name); |
| 138 | 917 | | return success |
| 138 | 918 | | ? "``" + methodIndex |
| 138 | 919 | | : "`" + typeGenericMap[type.Name]; |
| | 920 | | } |
| 399 | 921 | | else if (type.HasElementType) |
| 72 | 922 | | { |
| 72 | 923 | | string elementTypeString = GetXmlDocumenationFormattedString( |
| 72 | 924 | | type.GetElementType() ?? throw new ArgumentException($"{nameof(type)}.{nameof(Type.HasElementType)} && {nameof(t |
| 72 | 925 | | isMethodParameter, |
| 72 | 926 | | typeGenericMap, |
| 72 | 927 | | methodGenericMap); |
| | 928 | |
|
| 72 | 929 | | switch (type) |
| | 930 | | { |
| 72 | 931 | | case Type when type.IsPointer: |
| 8 | 932 | | return elementTypeString + "*"; |
| | 933 | |
|
| 64 | 934 | | case Type when type.IsByRef: |
| 12 | 935 | | return elementTypeString + "@"; |
| | 936 | |
|
| 52 | 937 | | case Type when type.IsArray: |
| 52 | 938 | | int rank = type.GetArrayRank(); |
| 52 | 939 | | string arrayDimensionsString = rank > 1 |
| 52 | 940 | | ? "[" + string.Join(",", Enumerable.Repeat("0:", rank)) + "]" |
| 52 | 941 | | : "[]"; |
| 52 | 942 | | return elementTypeString + arrayDimensionsString; |
| | 943 | |
|
| | 944 | | default: |
| 0 | 945 | | throw new TowelBugException($"{nameof(GetXmlDocumenationFormattedString)} encountered an unhandled element typ |
| | 946 | | } |
| | 947 | | } |
| | 948 | | else |
| 327 | 949 | | { |
| 327 | 950 | | string prefaceString = type.IsNested |
| 327 | 951 | | ? GetXmlDocumenationFormattedString( |
| 327 | 952 | | type.DeclaringType ?? throw new ArgumentException($"{nameof(type)}.{nameof(Type.IsNested)} && {nameof(type)}.{ |
| 327 | 953 | | isMethodParameter, |
| 327 | 954 | | typeGenericMap, |
| 327 | 955 | | methodGenericMap) + "." |
| 327 | 956 | | : type.Namespace + "."; |
| | 957 | |
|
| 327 | 958 | | string typeNameString = isMethodParameter |
| 327 | 959 | | ? typeNameString = Regex.Replace(type.Name, @"`\d+", string.Empty) |
| 327 | 960 | | : typeNameString = type.Name; |
| | 961 | |
|
| 327 | 962 | | string genericArgumentsString = type.IsGenericType && isMethodParameter |
| 327 | 963 | | ? "{" + string.Join(",", |
| 327 | 964 | | type.GetGenericArguments().Select(argument => |
| 40 | 965 | | GetXmlDocumenationFormattedString( |
| 40 | 966 | | argument, |
| 40 | 967 | | isMethodParameter, |
| 40 | 968 | | typeGenericMap, |
| 40 | 969 | | methodGenericMap)) |
| 327 | 970 | | ) + "}" |
| 327 | 971 | | : string.Empty; |
| | 972 | |
|
| 327 | 973 | | return prefaceString + typeNameString + genericArgumentsString; |
| | 974 | | } |
| 537 | 975 | | } |
| | 976 | |
|
| | 977 | | internal static string GetXmlNameTypeSegment(string typeFullNameString) => |
| 140 | 978 | | Regex.Replace(typeFullNameString, @"\[.*\]", string.Empty).Replace('+', '.'); |
| | 979 | |
|
| | 980 | | #endregion |
| | 981 | |
|
| | 982 | | #region GetXmlDocumentation |
| | 983 | |
|
| 1 | 984 | | internal static object xmlCacheLock = new(); |
| 1 | 985 | | internal static ISet<Assembly> loadedAssemblies = SetHashLinked.New<Assembly>(); |
| 1 | 986 | | internal static MapHashLinked<string, string, StringEquate, StringHash> loadedXmlDocumentation = new(); |
| | 987 | |
|
| | 988 | | internal static bool LoadXmlDocumentation(Assembly assembly) |
| 204 | 989 | | { |
| 204 | 990 | | if (loadedAssemblies.Contains(assembly)) |
| 196 | 991 | | { |
| 196 | 992 | | return false; |
| | 993 | | } |
| 8 | 994 | | bool newContent = false; |
| 8 | 995 | | string? directoryPath = assembly.GetDirectoryPath(); |
| 8 | 996 | | if (directoryPath is not null) |
| 8 | 997 | | { |
| 8 | 998 | | string xmlFilePath = Path.Combine(directoryPath, assembly.GetName().Name + ".xml"); |
| 8 | 999 | | if (File.Exists(xmlFilePath)) |
| 7 | 1000 | | { |
| 7 | 1001 | | using StreamReader streamReader = new(xmlFilePath); |
| 7 | 1002 | | LoadXmlDocumentationNoLock(streamReader); |
| 7 | 1003 | | newContent = true; |
| 7 | 1004 | | } |
| 8 | 1005 | | } |
| 8 | 1006 | | loadedAssemblies.Add(assembly); |
| 8 | 1007 | | return newContent; |
| 204 | 1008 | | } |
| | 1009 | |
|
| | 1010 | | /// <summary>Loads the XML code documentation into memory so it can be accessed by extension methods on reflection typ |
| | 1011 | | /// <param name="xmlDocumentation">The content of the XML code documentation.</param> |
| | 1012 | | public static void LoadXmlDocumentation(string xmlDocumentation) |
| 1 | 1013 | | { |
| 1 | 1014 | | using StringReader stringReader = new(xmlDocumentation); |
| 1 | 1015 | | LoadXmlDocumentation(stringReader); |
| 2 | 1016 | | } |
| | 1017 | |
|
| | 1018 | | /// <summary>Loads the XML code documentation into memory so it can be accessed by extension methods on reflection typ |
| | 1019 | | /// <param name="textReader">The text reader to process in an XmlReader.</param> |
| | 1020 | | public static void LoadXmlDocumentation(TextReader textReader) |
| 1 | 1021 | | { |
| 1 | 1022 | | lock (xmlCacheLock) |
| 1 | 1023 | | { |
| 1 | 1024 | | LoadXmlDocumentationNoLock(textReader); |
| 1 | 1025 | | } |
| 1 | 1026 | | } |
| | 1027 | |
|
| | 1028 | | internal static void LoadXmlDocumentationNoLock(TextReader textReader) |
| 8 | 1029 | | { |
| 8 | 1030 | | using XmlReader xmlReader = XmlReader.Create(textReader); |
| 1184 | 1031 | | while (xmlReader.Read()) |
| 1176 | 1032 | | { |
| 1176 | 1033 | | if (xmlReader.NodeType is XmlNodeType.Element && xmlReader.Name is "member") |
| 1032 | 1034 | | { |
| 1032 | 1035 | | string? rawName = xmlReader["name"]; |
| 1032 | 1036 | | if (!string.IsNullOrWhiteSpace(rawName)) |
| 1032 | 1037 | | { |
| 1032 | 1038 | | loadedXmlDocumentation[rawName] = xmlReader.ReadInnerXml(); |
| 1032 | 1039 | | } |
| 1032 | 1040 | | } |
| 1176 | 1041 | | } |
| 16 | 1042 | | } |
| | 1043 | |
|
| | 1044 | | /// <summary>Clears the currently loaded XML documentation.</summary> |
| | 1045 | | public static void ClearXmlDocumentation() |
| 9 | 1046 | | { |
| 9 | 1047 | | lock (xmlCacheLock) |
| 9 | 1048 | | { |
| 9 | 1049 | | loadedAssemblies.Clear(); |
| 9 | 1050 | | loadedXmlDocumentation.Clear(); |
| 9 | 1051 | | } |
| 9 | 1052 | | } |
| | 1053 | |
|
| | 1054 | | internal static string? GetDocumentation(string key, Assembly assembly) |
| 276 | 1055 | | { |
| 276 | 1056 | | lock (xmlCacheLock) |
| 276 | 1057 | | { |
| 276 | 1058 | | var (success, _, value) = loadedXmlDocumentation.TryGet(key); |
| 276 | 1059 | | if (success) |
| 248 | 1060 | | { |
| 248 | 1061 | | return value; |
| | 1062 | | } |
| 28 | 1063 | | else if (LoadXmlDocumentation(assembly)) |
| 3 | 1064 | | { |
| 3 | 1065 | | return loadedXmlDocumentation.TryGet(key).Value; |
| | 1066 | | } |
| | 1067 | | else |
| 25 | 1068 | | { |
| 25 | 1069 | | return null; |
| | 1070 | | } |
| | 1071 | | } |
| 276 | 1072 | | } |
| | 1073 | |
|
| | 1074 | | /// <summary>Gets the XML documentation on a type.</summary> |
| | 1075 | | /// <param name="type">The type to get the XML documentation of.</param> |
| | 1076 | | /// <returns>The XML documentation on the type.</returns> |
| | 1077 | | /// <remarks>The XML documentation must be loaded into memory for this function to work.</remarks> |
| | 1078 | | public static string? GetDocumentation(this Type type) |
| 41 | 1079 | | { |
| 42 | 1080 | | if (type is null) throw new ArgumentNullException(nameof(type)); |
| 40 | 1081 | | if (sourceof(type.FullName is null, out string c1)) throw new ArgumentException(c1, nameof(type)); |
| 40 | 1082 | | return GetDocumentation(type.GetXmlName(), type.Assembly); |
| 40 | 1083 | | } |
| | 1084 | |
|
| | 1085 | | /// <summary>Gets the XML documentation on a method.</summary> |
| | 1086 | | /// <param name="methodInfo">The method to get the XML documentation of.</param> |
| | 1087 | | /// <returns>The XML documentation on the method.</returns> |
| | 1088 | | /// <remarks>The XML documentation must be loaded into memory for this function to work.</remarks> |
| | 1089 | | public static string? GetDocumentation(this MethodInfo methodInfo) |
| 108 | 1090 | | { |
| 109 | 1091 | | if (methodInfo is null) throw new ArgumentNullException(nameof(methodInfo)); |
| 107 | 1092 | | if (sourceof(methodInfo.DeclaringType is null, out string c1)) throw new ArgumentException(c1, nameof(methodInfo)); |
| 107 | 1093 | | return GetDocumentation(methodInfo.GetXmlName(), methodInfo.DeclaringType!.Assembly); |
| 107 | 1094 | | } |
| | 1095 | |
|
| | 1096 | | /// <summary>Gets the XML documentation on a constructor.</summary> |
| | 1097 | | /// <param name="constructorInfo">The constructor to get the XML documentation of.</param> |
| | 1098 | | /// <returns>The XML documentation on the constructor.</returns> |
| | 1099 | | /// <remarks>The XML documentation must be loaded into memory for this function to work.</remarks> |
| | 1100 | | public static string? GetDocumentation(this ConstructorInfo constructorInfo) |
| 30 | 1101 | | { |
| 31 | 1102 | | if (constructorInfo is null) throw new ArgumentNullException(nameof(constructorInfo)); |
| 29 | 1103 | | if (sourceof(constructorInfo.DeclaringType is null, out string c1)) throw new ArgumentException(c1, nameof(construct |
| 29 | 1104 | | return GetDocumentation(constructorInfo.GetXmlName(), constructorInfo.DeclaringType!.Assembly); |
| 29 | 1105 | | } |
| | 1106 | |
|
| | 1107 | | /// <summary>Gets the XML documentation on a property.</summary> |
| | 1108 | | /// <param name="propertyInfo">The property to get the XML documentation of.</param> |
| | 1109 | | /// <returns>The XML documentation on the property.</returns> |
| | 1110 | | /// <remarks>The XML documentation must be loaded into memory for this function to work.</remarks> |
| | 1111 | | public static string? GetDocumentation(this PropertyInfo propertyInfo) |
| 41 | 1112 | | { |
| 42 | 1113 | | if (propertyInfo is null) throw new ArgumentNullException(nameof(propertyInfo)); |
| 40 | 1114 | | if (sourceof(propertyInfo.DeclaringType is null, out string c1)) throw new ArgumentException(c1, nameof(propertyInfo |
| 40 | 1115 | | if (sourceof(propertyInfo.DeclaringType!.FullName is null, out string c2)) throw new ArgumentException(c2, nameof(pr |
| 40 | 1116 | | return GetDocumentation(propertyInfo.GetXmlName(), propertyInfo.DeclaringType.Assembly); |
| 40 | 1117 | | } |
| | 1118 | |
|
| | 1119 | | /// <summary>Gets the XML documentation on a field.</summary> |
| | 1120 | | /// <param name="fieldInfo">The field to get the XML documentation of.</param> |
| | 1121 | | /// <returns>The XML documentation on the field.</returns> |
| | 1122 | | /// <remarks>The XML documentation must be loaded into memory for this function to work.</remarks> |
| | 1123 | | public static string? GetDocumentation(this FieldInfo fieldInfo) |
| 43 | 1124 | | { |
| 44 | 1125 | | if (fieldInfo is null) throw new ArgumentNullException(nameof(fieldInfo)); |
| 42 | 1126 | | if (sourceof(fieldInfo.DeclaringType is null, out string c1)) throw new ArgumentException(c1, nameof(fieldInfo)); |
| 42 | 1127 | | if (sourceof(fieldInfo.DeclaringType!.FullName is null, out string c2)) throw new ArgumentException(c2, nameof(field |
| 42 | 1128 | | return GetDocumentation(fieldInfo.GetXmlName(), fieldInfo.DeclaringType.Assembly); |
| 42 | 1129 | | } |
| | 1130 | |
|
| | 1131 | | /// <summary>Gets the XML documentation on an event.</summary> |
| | 1132 | | /// <param name="eventInfo">The event to get the XML documentation of.</param> |
| | 1133 | | /// <returns>The XML documentation on the event.</returns> |
| | 1134 | | /// <remarks>The XML documentation must be loaded into memory for this function to work.</remarks> |
| | 1135 | | public static string? GetDocumentation(this EventInfo eventInfo) |
| 19 | 1136 | | { |
| 20 | 1137 | | if (eventInfo is null) throw new ArgumentNullException(nameof(eventInfo)); |
| 18 | 1138 | | if (sourceof(eventInfo.DeclaringType is null, out string c1)) throw new ArgumentException(c1, nameof(eventInfo)); |
| 18 | 1139 | | if (sourceof(eventInfo.DeclaringType!.FullName is null, out string c2)) throw new ArgumentException(c2, nameof(event |
| 18 | 1140 | | return GetDocumentation(eventInfo.GetXmlName(), eventInfo.DeclaringType.Assembly); |
| 18 | 1141 | | } |
| | 1142 | |
|
| | 1143 | | /// <summary>Gets the XML documentation on a member.</summary> |
| | 1144 | | /// <param name="memberInfo">The member to get the XML documentation of.</param> |
| | 1145 | | /// <returns>The XML documentation on the member.</returns> |
| | 1146 | | /// <remarks>The XML documentation must be loaded into memory for this function to work.</remarks> |
| | 1147 | | public static string? GetDocumentation(this MemberInfo memberInfo) |
| 151 | 1148 | | { |
| 151 | 1149 | | switch (memberInfo) |
| | 1150 | | { |
| | 1151 | | case FieldInfo fieldInfo: |
| 23 | 1152 | | if (sourceof(fieldInfo.DeclaringType is null, out string c1)) throw new ArgumentException(c1, nameof(memberInfo) |
| 23 | 1153 | | if (sourceof(fieldInfo.DeclaringType!.FullName is null, out string c2)) throw new ArgumentException(c2, nameof(m |
| 23 | 1154 | | return fieldInfo.GetDocumentation(); |
| | 1155 | | case PropertyInfo propertyInfo: |
| 22 | 1156 | | if (sourceof(propertyInfo.DeclaringType is null, out string c3)) throw new ArgumentException(c3, nameof(memberIn |
| 22 | 1157 | | if (sourceof(propertyInfo.DeclaringType!.FullName is null, out string c4)) throw new ArgumentException(c4, nameo |
| 22 | 1158 | | return propertyInfo.GetDocumentation(); |
| | 1159 | | case EventInfo eventInfo: |
| 9 | 1160 | | if (sourceof(eventInfo.DeclaringType is null, out string c5)) throw new ArgumentException(c5, nameof(memberInfo) |
| 9 | 1161 | | if (sourceof(eventInfo.DeclaringType!.FullName is null, out string c6)) throw new ArgumentException(c6, nameof(m |
| 9 | 1162 | | return eventInfo.GetDocumentation(); |
| | 1163 | | case ConstructorInfo constructorInfo: |
| 15 | 1164 | | if (sourceof(constructorInfo.DeclaringType is null, out string c7)) throw new ArgumentException(c7, nameof(membe |
| 15 | 1165 | | return constructorInfo.GetDocumentation(); |
| | 1166 | | case MethodInfo methodInfo: |
| 61 | 1167 | | if (sourceof(methodInfo.DeclaringType is null, out string c8)) throw new ArgumentException(c8, nameof(memberInfo |
| 61 | 1168 | | return methodInfo.GetDocumentation(); |
| | 1169 | | case Type type: |
| 20 | 1170 | | if (sourceof(type.FullName is null, out string c9)) throw new ArgumentException(c9, nameof(memberInfo)); |
| 20 | 1171 | | return type.GetDocumentation(); |
| | 1172 | | case null: |
| 1 | 1173 | | throw new ArgumentNullException(nameof(memberInfo)); |
| | 1174 | | default: |
| 0 | 1175 | | throw new NotImplementedException($"{nameof(GetDocumentation)} encountered an unhandled {nameof(MemberInfo)} typ |
| | 1176 | | } |
| 150 | 1177 | | } |
| | 1178 | |
|
| | 1179 | | /// <summary>Gets the XML documentation for a parameter.</summary> |
| | 1180 | | /// <param name="parameterInfo">The parameter to get the XML documentation for.</param> |
| | 1181 | | /// <returns>The XML documenation of the parameter.</returns> |
| | 1182 | | public static string? GetDocumentation(this ParameterInfo parameterInfo) |
| 2 | 1183 | | { |
| 3 | 1184 | | if (parameterInfo is null) throw new ArgumentNullException(nameof(parameterInfo)); |
| 1 | 1185 | | string? memberDocumentation = parameterInfo.Member.GetDocumentation(); |
| 1 | 1186 | | if (memberDocumentation is not null) |
| 1 | 1187 | | { |
| 1 | 1188 | | string regexPattern = |
| 1 | 1189 | | Regex.Escape($@"<param name=""{parameterInfo.Name}"">") + |
| 1 | 1190 | | ".*?" + |
| 1 | 1191 | | Regex.Escape($@"</param>"); |
| | 1192 | |
|
| 1 | 1193 | | Match match = Regex.Match(memberDocumentation, regexPattern); |
| 1 | 1194 | | if (match.Success) |
| 1 | 1195 | | { |
| 1 | 1196 | | return match.Value; |
| | 1197 | | } |
| 0 | 1198 | | } |
| 0 | 1199 | | return null; |
| 1 | 1200 | | } |
| | 1201 | |
|
| | 1202 | | #endregion |
| | 1203 | |
|
| | 1204 | | #region System.Reflection.MethodBase |
| | 1205 | |
|
| | 1206 | | /// <summary>Determines if a method is a local function.</summary> |
| | 1207 | | /// <param name="methodBase">The method to determine if it is a local function.</param> |
| | 1208 | | /// <returns>True if the method is a local function. False if not.</returns> |
| | 1209 | | public static bool IsLocalFunction(this MethodBase methodBase) => |
| 26 | 1210 | | Regex.Match(methodBase.Name, @"g__.+\|\d+_\d+").Success; |
| | 1211 | |
|
| | 1212 | | #endregion |
| | 1213 | | } |