| | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | 2 | | using System.Reflection; |
| | 3 | | using System.Text; |
| | 4 | | using DotNetApiDiff.Interfaces; |
| | 5 | | using Microsoft.Extensions.Logging; |
| | 6 | |
|
| | 7 | | namespace DotNetApiDiff.ApiExtraction; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Builds normalized signatures for API members to enable consistent comparison |
| | 11 | | /// </summary> |
| | 12 | | public class MemberSignatureBuilder : IMemberSignatureBuilder |
| | 13 | | { |
| | 14 | | private readonly ILogger<MemberSignatureBuilder> _logger; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Creates a new instance of the MemberSignatureBuilder |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="logger">Logger for diagnostic information</param> |
| 30 | 20 | | public MemberSignatureBuilder(ILogger<MemberSignatureBuilder> logger) |
| 30 | 21 | | { |
| 30 | 22 | | _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 29 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Builds a normalized signature for a method |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="method">Method to build signature for</param> |
| | 29 | | /// <returns>Normalized method signature</returns> |
| | 30 | | public string BuildMethodSignature(MethodInfo method) |
| 7 | 31 | | { |
| 7 | 32 | | if (method == null) |
| 1 | 33 | | { |
| 1 | 34 | | throw new ArgumentNullException(nameof(method)); |
| | 35 | | } |
| | 36 | |
|
| | 37 | | try |
| 6 | 38 | | { |
| 6 | 39 | | var signature = new StringBuilder(); |
| | 40 | |
|
| | 41 | | // Add accessibility |
| 6 | 42 | | signature.Append(GetAccessibilityString(method)); |
| 6 | 43 | | signature.Append(' '); |
| | 44 | |
|
| | 45 | | // Add static/virtual/abstract modifiers |
| 6 | 46 | | if (method.IsStatic) |
| 1 | 47 | | { |
| 1 | 48 | | signature.Append("static "); |
| 1 | 49 | | } |
| 5 | 50 | | else if (method.IsVirtual && !method.IsFinal) |
| 1 | 51 | | { |
| 1 | 52 | | if (method.IsAbstract) |
| 0 | 53 | | { |
| 0 | 54 | | signature.Append("abstract "); |
| 0 | 55 | | } |
| 1 | 56 | | else if (method.DeclaringType?.IsInterface == false) |
| 1 | 57 | | { |
| 1 | 58 | | signature.Append("virtual "); |
| 1 | 59 | | } |
| 1 | 60 | | } |
| 4 | 61 | | else if (method.IsFinal && method.IsVirtual) |
| 0 | 62 | | { |
| 0 | 63 | | signature.Append("sealed override "); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | // Add return type |
| 6 | 67 | | signature.Append(GetTypeName(method.ReturnType)); |
| 6 | 68 | | signature.Append(' '); |
| | 69 | |
|
| | 70 | | // Add method name |
| 6 | 71 | | signature.Append(method.Name); |
| | 72 | |
|
| | 73 | | // Add generic type parameters if any |
| 6 | 74 | | if (method.IsGenericMethod) |
| 1 | 75 | | { |
| 1 | 76 | | signature.Append('<'); |
| 1 | 77 | | var genericArgs = method.GetGenericArguments(); |
| 4 | 78 | | for (int i = 0; i < genericArgs.Length; i++) |
| 1 | 79 | | { |
| 1 | 80 | | if (i > 0) |
| 0 | 81 | | { |
| 0 | 82 | | signature.Append(", "); |
| 0 | 83 | | } |
| | 84 | |
|
| 1 | 85 | | signature.Append(GetGenericParameterName(genericArgs[i])); |
| 1 | 86 | | } |
| | 87 | |
|
| 1 | 88 | | signature.Append('>'); |
| 1 | 89 | | } |
| | 90 | |
|
| | 91 | | // Add parameters |
| 6 | 92 | | signature.Append('('); |
| 6 | 93 | | var parameters = method.GetParameters(); |
| 18 | 94 | | for (int i = 0; i < parameters.Length; i++) |
| 3 | 95 | | { |
| 3 | 96 | | if (i > 0) |
| 1 | 97 | | { |
| 1 | 98 | | signature.Append(", "); |
| 1 | 99 | | } |
| | 100 | |
|
| 3 | 101 | | signature.Append(BuildParameterSignature(parameters[i])); |
| 3 | 102 | | } |
| | 103 | |
|
| 6 | 104 | | signature.Append(')'); |
| | 105 | |
|
| 6 | 106 | | return signature.ToString(); |
| | 107 | | } |
| 0 | 108 | | catch (Exception ex) |
| 0 | 109 | | { |
| 0 | 110 | | _logger.LogError(ex, "Error building signature for method {MethodName}", method.Name); |
| 0 | 111 | | return $"Error: {method.Name}"; |
| | 112 | | } |
| 6 | 113 | | } |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// Builds a normalized signature for a property |
| | 117 | | /// </summary> |
| | 118 | | /// <param name="property">Property to build signature for</param> |
| | 119 | | /// <returns>Normalized property signature</returns> |
| | 120 | | public string BuildPropertySignature(PropertyInfo property) |
| 3 | 121 | | { |
| 3 | 122 | | if (property == null) |
| 1 | 123 | | { |
| 1 | 124 | | throw new ArgumentNullException(nameof(property)); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | try |
| 2 | 128 | | { |
| 2 | 129 | | var signature = new StringBuilder(); |
| | 130 | |
|
| | 131 | | // Get the most accessible accessor between getter and setter |
| 2 | 132 | | var getMethod = property.GetMethod; |
| 2 | 133 | | var setMethod = property.SetMethod; |
| | 134 | |
|
| | 135 | | // Add accessibility |
| 2 | 136 | | if (getMethod != null && setMethod != null) |
| 1 | 137 | | { |
| | 138 | | // Use the most accessible between get and set |
| 1 | 139 | | var getAccess = GetAccessibilityString(getMethod); |
| 1 | 140 | | var setAccess = GetAccessibilityString(setMethod); |
| 1 | 141 | | signature.Append(GetMostAccessible(getAccess, setAccess)); |
| 1 | 142 | | } |
| 1 | 143 | | else if (getMethod != null) |
| 1 | 144 | | { |
| 1 | 145 | | signature.Append(GetAccessibilityString(getMethod)); |
| 1 | 146 | | } |
| 0 | 147 | | else if (setMethod != null) |
| 0 | 148 | | { |
| 0 | 149 | | signature.Append(GetAccessibilityString(setMethod)); |
| 0 | 150 | | } |
| | 151 | | else |
| 0 | 152 | | { |
| 0 | 153 | | signature.Append("private"); |
| 0 | 154 | | } |
| | 155 | |
|
| 2 | 156 | | signature.Append(' '); |
| | 157 | |
|
| | 158 | | // Add static modifier if applicable |
| 2 | 159 | | if ((getMethod != null && getMethod.IsStatic) || (setMethod != null && setMethod.IsStatic)) |
| 0 | 160 | | { |
| 0 | 161 | | signature.Append("static "); |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | // Add virtual/abstract modifiers |
| 2 | 165 | | if (getMethod != null && getMethod.IsVirtual && !getMethod.IsFinal) |
| 0 | 166 | | { |
| 0 | 167 | | if (getMethod.IsAbstract) |
| 0 | 168 | | { |
| 0 | 169 | | signature.Append("abstract "); |
| 0 | 170 | | } |
| 0 | 171 | | else if (property.DeclaringType?.IsInterface == false) |
| 0 | 172 | | { |
| 0 | 173 | | signature.Append("virtual "); |
| 0 | 174 | | } |
| 0 | 175 | | } |
| 2 | 176 | | else if (getMethod != null && getMethod.IsFinal && getMethod.IsVirtual) |
| 0 | 177 | | { |
| 0 | 178 | | signature.Append("sealed override "); |
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | // Add property type |
| 2 | 182 | | signature.Append(GetTypeName(property.PropertyType)); |
| 2 | 183 | | signature.Append(' '); |
| | 184 | |
|
| | 185 | | // Add property name |
| 2 | 186 | | signature.Append(property.Name); |
| | 187 | |
|
| | 188 | | // Add accessors |
| 2 | 189 | | signature.Append(" { "); |
| 2 | 190 | | if (getMethod != null) |
| 2 | 191 | | { |
| 2 | 192 | | signature.Append("get; "); |
| 2 | 193 | | } |
| | 194 | |
|
| 2 | 195 | | if (setMethod != null) |
| 1 | 196 | | { |
| 1 | 197 | | signature.Append("set; "); |
| 1 | 198 | | } |
| | 199 | |
|
| 2 | 200 | | signature.Append('}'); |
| | 201 | |
|
| 2 | 202 | | return signature.ToString(); |
| | 203 | | } |
| 0 | 204 | | catch (Exception ex) |
| 0 | 205 | | { |
| 0 | 206 | | _logger.LogError(ex, "Error building signature for property {PropertyName}", property.Name); |
| 0 | 207 | | return $"Error: {property.Name}"; |
| | 208 | | } |
| 2 | 209 | | } |
| | 210 | |
|
| | 211 | | /// <summary> |
| | 212 | | /// Builds a normalized signature for a field |
| | 213 | | /// </summary> |
| | 214 | | /// <param name="field">Field to build signature for</param> |
| | 215 | | /// <returns>Normalized field signature</returns> |
| | 216 | | public string BuildFieldSignature(FieldInfo field) |
| 4 | 217 | | { |
| 4 | 218 | | if (field == null) |
| 1 | 219 | | { |
| 1 | 220 | | throw new ArgumentNullException(nameof(field)); |
| | 221 | | } |
| | 222 | |
|
| | 223 | | try |
| 3 | 224 | | { |
| 3 | 225 | | var signature = new StringBuilder(); |
| | 226 | |
|
| | 227 | | // Add accessibility |
| 3 | 228 | | signature.Append(GetAccessibilityString(field)); |
| 3 | 229 | | signature.Append(' '); |
| | 230 | |
|
| | 231 | | // Add static/readonly/const modifiers |
| 3 | 232 | | if (field.IsStatic) |
| 1 | 233 | | { |
| 1 | 234 | | signature.Append("static "); |
| 1 | 235 | | } |
| | 236 | |
|
| 3 | 237 | | if (field.IsInitOnly) |
| 1 | 238 | | { |
| 1 | 239 | | signature.Append("readonly "); |
| 1 | 240 | | } |
| | 241 | |
|
| 3 | 242 | | if (field.IsLiteral) |
| 0 | 243 | | { |
| 0 | 244 | | signature.Append("const "); |
| 0 | 245 | | } |
| | 246 | |
|
| | 247 | | // Add field type |
| 3 | 248 | | signature.Append(GetTypeName(field.FieldType)); |
| 3 | 249 | | signature.Append(' '); |
| | 250 | |
|
| | 251 | | // Add field name |
| 3 | 252 | | signature.Append(field.Name); |
| | 253 | |
|
| 3 | 254 | | return signature.ToString(); |
| | 255 | | } |
| 0 | 256 | | catch (Exception ex) |
| 0 | 257 | | { |
| 0 | 258 | | _logger.LogError(ex, "Error building signature for field {FieldName}", field.Name); |
| 0 | 259 | | return $"Error: {field.Name}"; |
| | 260 | | } |
| 3 | 261 | | } |
| | 262 | |
|
| | 263 | | /// <summary> |
| | 264 | | /// Builds a normalized signature for an event |
| | 265 | | /// </summary> |
| | 266 | | /// <param name="eventInfo">Event to build signature for</param> |
| | 267 | | /// <returns>Normalized event signature</returns> |
| | 268 | | public string BuildEventSignature(EventInfo eventInfo) |
| 2 | 269 | | { |
| 2 | 270 | | if (eventInfo == null) |
| 1 | 271 | | { |
| 1 | 272 | | throw new ArgumentNullException(nameof(eventInfo)); |
| | 273 | | } |
| | 274 | |
|
| | 275 | | try |
| 1 | 276 | | { |
| 1 | 277 | | var signature = new StringBuilder(); |
| | 278 | |
|
| | 279 | | // Get the add method to determine accessibility |
| 1 | 280 | | var addMethod = eventInfo.AddMethod; |
| | 281 | |
|
| | 282 | | // Add accessibility |
| 1 | 283 | | if (addMethod != null) |
| 1 | 284 | | { |
| 1 | 285 | | signature.Append(GetAccessibilityString(addMethod)); |
| 1 | 286 | | signature.Append(' '); |
| | 287 | |
|
| | 288 | | // Add static modifier if applicable |
| 1 | 289 | | if (addMethod.IsStatic) |
| 0 | 290 | | { |
| 0 | 291 | | signature.Append("static "); |
| 0 | 292 | | } |
| | 293 | |
|
| | 294 | | // Add virtual/abstract modifiers |
| 1 | 295 | | if (addMethod.IsVirtual && !addMethod.IsFinal) |
| 0 | 296 | | { |
| 0 | 297 | | if (addMethod.IsAbstract) |
| 0 | 298 | | { |
| 0 | 299 | | signature.Append("abstract "); |
| 0 | 300 | | } |
| 0 | 301 | | else if (eventInfo.DeclaringType?.IsInterface == false) |
| 0 | 302 | | { |
| 0 | 303 | | signature.Append("virtual "); |
| 0 | 304 | | } |
| 0 | 305 | | } |
| 1 | 306 | | else if (addMethod.IsFinal && addMethod.IsVirtual) |
| 0 | 307 | | { |
| 0 | 308 | | signature.Append("sealed override "); |
| 0 | 309 | | } |
| 1 | 310 | | } |
| | 311 | |
|
| | 312 | | // Add event keyword and event handler type |
| 1 | 313 | | signature.Append("event "); |
| 1 | 314 | | signature.Append(GetTypeName(eventInfo.EventHandlerType ?? typeof(object))); |
| 1 | 315 | | signature.Append(' '); |
| | 316 | |
|
| | 317 | | // Add event name |
| 1 | 318 | | signature.Append(eventInfo.Name); |
| | 319 | |
|
| 1 | 320 | | return signature.ToString(); |
| | 321 | | } |
| 0 | 322 | | catch (Exception ex) |
| 0 | 323 | | { |
| 0 | 324 | | _logger.LogError(ex, "Error building signature for event {EventName}", eventInfo.Name); |
| 0 | 325 | | return $"Error: {eventInfo.Name}"; |
| | 326 | | } |
| 1 | 327 | | } |
| | 328 | |
|
| | 329 | | /// <summary> |
| | 330 | | /// Builds a normalized signature for a constructor |
| | 331 | | /// </summary> |
| | 332 | | /// <param name="constructor">Constructor to build signature for</param> |
| | 333 | | /// <returns>Normalized constructor signature</returns> |
| | 334 | | public string BuildConstructorSignature(ConstructorInfo constructor) |
| 3 | 335 | | { |
| 3 | 336 | | if (constructor == null) |
| 1 | 337 | | { |
| 1 | 338 | | throw new ArgumentNullException(nameof(constructor)); |
| | 339 | | } |
| | 340 | |
|
| | 341 | | try |
| 2 | 342 | | { |
| 2 | 343 | | var signature = new StringBuilder(); |
| | 344 | |
|
| | 345 | | // Add accessibility |
| 2 | 346 | | signature.Append(GetAccessibilityString(constructor)); |
| 2 | 347 | | signature.Append(' '); |
| | 348 | |
|
| | 349 | | // Add static modifier for static constructors |
| 2 | 350 | | if (constructor.IsStatic) |
| 0 | 351 | | { |
| 0 | 352 | | signature.Append("static "); |
| 0 | 353 | | } |
| | 354 | |
|
| | 355 | | // Add constructor name (use declaring type name) |
| 2 | 356 | | var typeName = constructor.DeclaringType?.Name ?? "Unknown"; |
| 2 | 357 | | if (typeName.Contains('`')) |
| 0 | 358 | | { |
| 0 | 359 | | typeName = typeName.Substring(0, typeName.IndexOf('`')); |
| 0 | 360 | | } |
| | 361 | |
|
| 2 | 362 | | signature.Append(typeName); |
| | 363 | |
|
| | 364 | | // Add parameters |
| 2 | 365 | | signature.Append('('); |
| 2 | 366 | | var parameters = constructor.GetParameters(); |
| 6 | 367 | | for (int i = 0; i < parameters.Length; i++) |
| 1 | 368 | | { |
| 1 | 369 | | if (i > 0) |
| 0 | 370 | | { |
| 0 | 371 | | signature.Append(", "); |
| 0 | 372 | | } |
| | 373 | |
|
| 1 | 374 | | signature.Append(BuildParameterSignature(parameters[i])); |
| 1 | 375 | | } |
| | 376 | |
|
| 2 | 377 | | signature.Append(')'); |
| | 378 | |
|
| 2 | 379 | | return signature.ToString(); |
| | 380 | | } |
| 0 | 381 | | catch (Exception ex) |
| 0 | 382 | | { |
| 0 | 383 | | _logger.LogError( |
| 0 | 384 | | ex, |
| 0 | 385 | | "Error building signature for constructor in type {TypeName}", |
| 0 | 386 | | constructor.DeclaringType?.Name ?? "Unknown"); |
| 0 | 387 | | return $"Error: Constructor in {constructor.DeclaringType?.Name ?? "Unknown"}"; |
| | 388 | | } |
| 2 | 389 | | } |
| | 390 | |
|
| | 391 | | /// <summary> |
| | 392 | | /// Builds a normalized signature for a type |
| | 393 | | /// </summary> |
| | 394 | | /// <param name="type">Type to build signature for</param> |
| | 395 | | /// <returns>Normalized type signature</returns> |
| | 396 | | public string BuildTypeSignature(Type type) |
| 0 | 397 | | { |
| 0 | 398 | | if (type == null) |
| 0 | 399 | | { |
| 0 | 400 | | throw new ArgumentNullException(nameof(type)); |
| | 401 | | } |
| | 402 | |
|
| | 403 | | try |
| 0 | 404 | | { |
| 0 | 405 | | var signature = new StringBuilder(); |
| | 406 | |
|
| | 407 | | // Add accessibility |
| 0 | 408 | | signature.Append(GetTypeAccessibilityString(type)); |
| 0 | 409 | | signature.Append(' '); |
| | 410 | |
|
| | 411 | | // Add sealed/abstract modifiers |
| 0 | 412 | | if (type.IsSealed && !type.IsValueType && !type.IsEnum) |
| 0 | 413 | | { |
| 0 | 414 | | if (type.IsAbstract) |
| 0 | 415 | | { |
| 0 | 416 | | signature.Append("static "); |
| 0 | 417 | | } |
| | 418 | | else |
| 0 | 419 | | { |
| 0 | 420 | | signature.Append("sealed "); |
| 0 | 421 | | } |
| 0 | 422 | | } |
| 0 | 423 | | else if (type.IsAbstract && !type.IsInterface) |
| 0 | 424 | | { |
| 0 | 425 | | signature.Append("abstract "); |
| 0 | 426 | | } |
| | 427 | |
|
| | 428 | | // Add type kind (class, struct, interface, enum, delegate) |
| 0 | 429 | | if (type.IsInterface) |
| 0 | 430 | | { |
| 0 | 431 | | signature.Append("interface "); |
| 0 | 432 | | } |
| 0 | 433 | | else if (type.IsEnum) |
| 0 | 434 | | { |
| 0 | 435 | | signature.Append("enum "); |
| 0 | 436 | | } |
| 0 | 437 | | else if (type.IsValueType) |
| 0 | 438 | | { |
| 0 | 439 | | signature.Append("struct "); |
| 0 | 440 | | } |
| 0 | 441 | | else if (type.IsSubclassOf(typeof(MulticastDelegate))) |
| 0 | 442 | | { |
| 0 | 443 | | signature.Append("delegate "); |
| 0 | 444 | | } |
| | 445 | | else |
| 0 | 446 | | { |
| 0 | 447 | | signature.Append("class "); |
| 0 | 448 | | } |
| | 449 | |
|
| | 450 | | // Add type name |
| 0 | 451 | | signature.Append(type.Name); |
| | 452 | |
|
| | 453 | | // Add generic parameters if any |
| 0 | 454 | | if (type.IsGenericType && !type.IsGenericTypeDefinition) |
| 0 | 455 | | { |
| | 456 | | // For constructed generic types, include the type arguments |
| 0 | 457 | | var genericArgs = type.GetGenericArguments(); |
| 0 | 458 | | signature.Append('<'); |
| 0 | 459 | | for (int i = 0; i < genericArgs.Length; i++) |
| 0 | 460 | | { |
| 0 | 461 | | if (i > 0) |
| 0 | 462 | | { |
| 0 | 463 | | signature.Append(", "); |
| 0 | 464 | | } |
| | 465 | |
|
| 0 | 466 | | signature.Append(GetTypeName(genericArgs[i])); |
| 0 | 467 | | } |
| | 468 | |
|
| 0 | 469 | | signature.Append('>'); |
| 0 | 470 | | } |
| 0 | 471 | | else if (type.IsGenericTypeDefinition) |
| 0 | 472 | | { |
| | 473 | | // For generic type definitions, include the type parameter names |
| 0 | 474 | | var genericArgs = type.GetGenericArguments(); |
| 0 | 475 | | signature.Append('<'); |
| 0 | 476 | | for (int i = 0; i < genericArgs.Length; i++) |
| 0 | 477 | | { |
| 0 | 478 | | if (i > 0) |
| 0 | 479 | | { |
| 0 | 480 | | signature.Append(", "); |
| 0 | 481 | | } |
| | 482 | |
|
| 0 | 483 | | signature.Append(GetGenericParameterName(genericArgs[i])); |
| 0 | 484 | | } |
| | 485 | |
|
| 0 | 486 | | signature.Append('>'); |
| 0 | 487 | | } |
| | 488 | |
|
| | 489 | | // Add base type if not Object or ValueType |
| 0 | 490 | | if (type.BaseType != null && type.BaseType != typeof(object) && type.BaseType != typeof(ValueType) && |
| 0 | 491 | | type.BaseType != typeof(Enum)) |
| 0 | 492 | | { |
| 0 | 493 | | signature.Append(" : "); |
| 0 | 494 | | signature.Append(GetTypeName(type.BaseType)); |
| 0 | 495 | | } |
| | 496 | |
|
| | 497 | | // Add implemented interfaces |
| 0 | 498 | | var interfaces = type.GetInterfaces(); |
| 0 | 499 | | if (interfaces.Length > 0 && !type.IsInterface) |
| 0 | 500 | | { |
| 0 | 501 | | if (type.BaseType == null || type.BaseType == typeof(object) || type.BaseType == typeof(ValueType) || |
| 0 | 502 | | type.BaseType == typeof(Enum)) |
| 0 | 503 | | { |
| 0 | 504 | | signature.Append(" : "); |
| 0 | 505 | | } |
| | 506 | | else |
| 0 | 507 | | { |
| 0 | 508 | | signature.Append(", "); |
| 0 | 509 | | } |
| | 510 | |
|
| 0 | 511 | | for (int i = 0; i < interfaces.Length; i++) |
| 0 | 512 | | { |
| 0 | 513 | | if (i > 0) |
| 0 | 514 | | { |
| 0 | 515 | | signature.Append(", "); |
| 0 | 516 | | } |
| | 517 | |
|
| 0 | 518 | | signature.Append(GetTypeName(interfaces[i])); |
| 0 | 519 | | } |
| 0 | 520 | | } |
| 0 | 521 | | else if (type.IsInterface && interfaces.Length > 0) |
| 0 | 522 | | { |
| | 523 | | // For interfaces, show base interfaces |
| 0 | 524 | | signature.Append(" : "); |
| 0 | 525 | | for (int i = 0; i < interfaces.Length; i++) |
| 0 | 526 | | { |
| 0 | 527 | | if (i > 0) |
| 0 | 528 | | { |
| 0 | 529 | | signature.Append(", "); |
| 0 | 530 | | } |
| | 531 | |
|
| 0 | 532 | | signature.Append(GetTypeName(interfaces[i])); |
| 0 | 533 | | } |
| 0 | 534 | | } |
| | 535 | |
|
| 0 | 536 | | return signature.ToString(); |
| | 537 | | } |
| 0 | 538 | | catch (Exception ex) |
| 0 | 539 | | { |
| 0 | 540 | | _logger.LogError(ex, "Error building signature for type {TypeName}", type.Name); |
| 0 | 541 | | return $"Error: {type.Name}"; |
| | 542 | | } |
| 0 | 543 | | } |
| | 544 | |
|
| | 545 | | /// <summary> |
| | 546 | | /// Builds a signature for a method parameter |
| | 547 | | /// </summary> |
| | 548 | | /// <param name="parameter">Parameter to build signature for</param> |
| | 549 | | /// <returns>Parameter signature</returns> |
| | 550 | | private string BuildParameterSignature(ParameterInfo parameter) |
| 4 | 551 | | { |
| 4 | 552 | | var signature = new StringBuilder(); |
| | 553 | |
|
| | 554 | | // Add parameter modifiers |
| 4 | 555 | | if (parameter.IsOut) |
| 0 | 556 | | { |
| 0 | 557 | | signature.Append("out "); |
| 0 | 558 | | } |
| 4 | 559 | | else if (parameter.ParameterType.IsByRef) |
| 0 | 560 | | { |
| 0 | 561 | | signature.Append("ref "); |
| 0 | 562 | | } |
| 4 | 563 | | else if (parameter.IsDefined(typeof(ParamArrayAttribute), false)) |
| 0 | 564 | | { |
| 0 | 565 | | signature.Append("params "); |
| 0 | 566 | | } |
| | 567 | |
|
| | 568 | | // Add parameter type |
| 4 | 569 | | var paramType = parameter.ParameterType; |
| 4 | 570 | | if (paramType.IsByRef) |
| 0 | 571 | | { |
| 0 | 572 | | paramType = paramType.GetElementType() ?? paramType; |
| 0 | 573 | | } |
| | 574 | |
|
| 4 | 575 | | signature.Append(GetTypeName(paramType)); |
| | 576 | |
|
| | 577 | | // Add parameter name |
| 4 | 578 | | signature.Append(' '); |
| 4 | 579 | | signature.Append(parameter.Name); |
| | 580 | |
|
| | 581 | | // Add default value if parameter is optional |
| 4 | 582 | | if (parameter.HasDefaultValue) |
| 0 | 583 | | { |
| 0 | 584 | | signature.Append(" = "); |
| 0 | 585 | | if (parameter.DefaultValue == null) |
| 0 | 586 | | { |
| 0 | 587 | | signature.Append("null"); |
| 0 | 588 | | } |
| 0 | 589 | | else if (parameter.DefaultValue is string stringValue) |
| 0 | 590 | | { |
| 0 | 591 | | signature.Append($"\"{stringValue}\""); |
| 0 | 592 | | } |
| | 593 | | else |
| 0 | 594 | | { |
| 0 | 595 | | signature.Append(parameter.DefaultValue); |
| 0 | 596 | | } |
| 0 | 597 | | } |
| | 598 | |
|
| 4 | 599 | | return signature.ToString(); |
| 4 | 600 | | } |
| | 601 | |
|
| | 602 | | /// <summary> |
| | 603 | | /// Gets a string representation of a type name |
| | 604 | | /// </summary> |
| | 605 | | /// <param name="type">Type to get name for</param> |
| | 606 | | /// <returns>Type name</returns> |
| | 607 | | private string GetTypeName(Type type) |
| 16 | 608 | | { |
| 16 | 609 | | if (type == null) |
| 0 | 610 | | { |
| 0 | 611 | | return "void"; |
| | 612 | | } |
| | 613 | |
|
| | 614 | | // Special type handling based on type characteristics |
| 16 | 615 | | switch (true) |
| | 616 | | { |
| 16 | 617 | | case object _ when type == typeof(void): |
| 5 | 618 | | return "void"; |
| | 619 | |
|
| 11 | 620 | | case object _ when type.IsByRef: |
| 0 | 621 | | return GetTypeName(type.GetElementType() ?? type); |
| | 622 | |
|
| 11 | 623 | | case object _ when type.IsArray: |
| 0 | 624 | | var elementType = type.GetElementType() ?? type; |
| 0 | 625 | | var rank = type.GetArrayRank(); |
| 0 | 626 | | if (rank == 1) |
| 0 | 627 | | { |
| 0 | 628 | | return $"{GetTypeName(elementType)}[]"; |
| | 629 | | } |
| | 630 | | else |
| 0 | 631 | | { |
| 0 | 632 | | var commas = new string(',', rank - 1); |
| 0 | 633 | | return $"{GetTypeName(elementType)}[{commas}]"; |
| | 634 | | } |
| | 635 | |
|
| 11 | 636 | | case object _ when type.IsPointer: |
| 0 | 637 | | return $"{GetTypeName(type.GetElementType() ?? type)}*"; |
| | 638 | |
|
| 11 | 639 | | case object _ when type.IsGenericParameter: |
| 2 | 640 | | return type.Name; |
| | 641 | |
|
| 9 | 642 | | case object _ when type.IsGenericType: |
| 0 | 643 | | var genericTypeDef = type.GetGenericTypeDefinition(); |
| 0 | 644 | | var genericArgs = type.GetGenericArguments(); |
| 0 | 645 | | var typeName = genericTypeDef.Name; |
| | 646 | |
|
| | 647 | | // Remove the `n suffix from generic type names |
| 0 | 648 | | if (typeName.Contains('`')) |
| 0 | 649 | | { |
| 0 | 650 | | typeName = typeName.Substring(0, typeName.IndexOf('`')); |
| 0 | 651 | | } |
| | 652 | |
|
| | 653 | | // Handle common generic collections |
| 0 | 654 | | if (genericTypeDef == typeof(Nullable<>)) |
| 0 | 655 | | { |
| 0 | 656 | | return $"{GetTypeName(genericArgs[0])}?"; |
| | 657 | | } |
| | 658 | |
|
| | 659 | | // For other generic types, use the standard format |
| 0 | 660 | | var sb = new StringBuilder(); |
| 0 | 661 | | sb.Append(typeName); |
| 0 | 662 | | sb.Append('<'); |
| | 663 | |
|
| 0 | 664 | | for (int i = 0; i < genericArgs.Length; i++) |
| 0 | 665 | | { |
| 0 | 666 | | if (i > 0) |
| 0 | 667 | | { |
| 0 | 668 | | sb.Append(", "); |
| 0 | 669 | | } |
| | 670 | |
|
| 0 | 671 | | sb.Append(GetTypeName(genericArgs[i])); |
| 0 | 672 | | } |
| | 673 | |
|
| 0 | 674 | | sb.Append('>'); |
| 0 | 675 | | return sb.ToString(); |
| | 676 | | } |
| | 677 | |
|
| | 678 | | // Handle primitive types with C# keywords using a direct switch on type |
| 9 | 679 | | var primitiveTypeName = type switch |
| 9 | 680 | | { |
| 9 | 681 | | Type t when t == typeof(bool) => "bool", |
| 9 | 682 | | Type t when t == typeof(byte) => "byte", |
| 9 | 683 | | Type t when t == typeof(sbyte) => "sbyte", |
| 9 | 684 | | Type t when t == typeof(char) => "char", |
| 9 | 685 | | Type t when t == typeof(decimal) => "decimal", |
| 9 | 686 | | Type t when t == typeof(double) => "double", |
| 9 | 687 | | Type t when t == typeof(float) => "float", |
| 10 | 688 | | Type t when t == typeof(int) => "int", |
| 8 | 689 | | Type t when t == typeof(uint) => "uint", |
| 8 | 690 | | Type t when t == typeof(long) => "long", |
| 8 | 691 | | Type t when t == typeof(ulong) => "ulong", |
| 8 | 692 | | Type t when t == typeof(short) => "short", |
| 8 | 693 | | Type t when t == typeof(ushort) => "ushort", |
| 15 | 694 | | Type t when t == typeof(string) => "string", |
| 1 | 695 | | Type t when t == typeof(object) => "object", |
| 1 | 696 | | _ => null // Not a primitive type |
| 9 | 697 | | }; |
| | 698 | |
|
| 9 | 699 | | if (primitiveTypeName != null) |
| 8 | 700 | | { |
| 8 | 701 | | return primitiveTypeName; |
| | 702 | | } |
| | 703 | |
|
| | 704 | | // For regular types, return the type name directly |
| 1 | 705 | | return type.Name; |
| 16 | 706 | | } |
| | 707 | |
|
| | 708 | | /// <summary> |
| | 709 | | /// Gets the name of a generic parameter |
| | 710 | | /// </summary> |
| | 711 | | /// <param name="type">Generic parameter type</param> |
| | 712 | | /// <returns>Generic parameter name</returns> |
| | 713 | | private string GetGenericParameterName(Type type) |
| 1 | 714 | | { |
| 1 | 715 | | if (!type.IsGenericParameter) |
| 0 | 716 | | { |
| 0 | 717 | | return GetTypeName(type); |
| | 718 | | } |
| | 719 | |
|
| 1 | 720 | | var constraints = new List<string>(); |
| | 721 | |
|
| | 722 | | // Add class/struct constraint |
| 1 | 723 | | if (type.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint)) |
| 0 | 724 | | { |
| 0 | 725 | | constraints.Add("class"); |
| 0 | 726 | | } |
| 1 | 727 | | else if (type.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint)) |
| 0 | 728 | | { |
| 0 | 729 | | constraints.Add("struct"); |
| 0 | 730 | | } |
| | 731 | |
|
| | 732 | | // Add new() constraint |
| 1 | 733 | | if (type.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint) && |
| 1 | 734 | | !type.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint)) |
| 0 | 735 | | { |
| 0 | 736 | | constraints.Add("new()"); |
| 0 | 737 | | } |
| | 738 | |
|
| | 739 | | // Add interface and base class constraints |
| 3 | 740 | | foreach (var constraint in type.GetGenericParameterConstraints()) |
| 0 | 741 | | { |
| 0 | 742 | | if (constraint != typeof(object)) |
| 0 | 743 | | { |
| 0 | 744 | | constraints.Add(GetTypeName(constraint)); |
| 0 | 745 | | } |
| 0 | 746 | | } |
| | 747 | |
|
| 1 | 748 | | if (constraints.Count == 0) |
| 1 | 749 | | { |
| 1 | 750 | | return type.Name; |
| | 751 | | } |
| | 752 | |
|
| 0 | 753 | | return $"{type.Name} : {string.Join(", ", constraints)}"; |
| 1 | 754 | | } |
| | 755 | |
|
| | 756 | | /// <summary> |
| | 757 | | /// Gets a string representation of a member's accessibility |
| | 758 | | /// </summary> |
| | 759 | | /// <param name="methodBase">Method or constructor to get accessibility for</param> |
| | 760 | | /// <returns>Accessibility string</returns> |
| | 761 | | private string GetAccessibilityString(MethodBase methodBase) |
| 12 | 762 | | { |
| 12 | 763 | | if (methodBase.IsPublic) |
| 11 | 764 | | { |
| 11 | 765 | | return "public"; |
| | 766 | | } |
| 1 | 767 | | else if (methodBase.IsFamily) |
| 0 | 768 | | { |
| 0 | 769 | | return "protected"; |
| | 770 | | } |
| 1 | 771 | | else if (methodBase.IsFamilyOrAssembly) |
| 0 | 772 | | { |
| 0 | 773 | | return "protected internal"; |
| | 774 | | } |
| 1 | 775 | | else if (methodBase.IsFamilyAndAssembly) |
| 0 | 776 | | { |
| 0 | 777 | | return "private protected"; |
| | 778 | | } |
| 1 | 779 | | else if (methodBase.IsAssembly) |
| 0 | 780 | | { |
| 0 | 781 | | return "internal"; |
| | 782 | | } |
| | 783 | | else |
| 1 | 784 | | { |
| 1 | 785 | | return "private"; |
| | 786 | | } |
| 12 | 787 | | } |
| | 788 | |
|
| | 789 | | /// <summary> |
| | 790 | | /// Gets a string representation of a field's accessibility |
| | 791 | | /// </summary> |
| | 792 | | /// <param name="field">Field to get accessibility for</param> |
| | 793 | | /// <returns>Accessibility string</returns> |
| | 794 | | private string GetAccessibilityString(FieldInfo field) |
| 3 | 795 | | { |
| 3 | 796 | | if (field.IsPublic) |
| 3 | 797 | | { |
| 3 | 798 | | return "public"; |
| | 799 | | } |
| 0 | 800 | | else if (field.IsFamily) |
| 0 | 801 | | { |
| 0 | 802 | | return "protected"; |
| | 803 | | } |
| 0 | 804 | | else if (field.IsFamilyOrAssembly) |
| 0 | 805 | | { |
| 0 | 806 | | return "protected internal"; |
| | 807 | | } |
| 0 | 808 | | else if (field.IsFamilyAndAssembly) |
| 0 | 809 | | { |
| 0 | 810 | | return "private protected"; |
| | 811 | | } |
| 0 | 812 | | else if (field.IsAssembly) |
| 0 | 813 | | { |
| 0 | 814 | | return "internal"; |
| | 815 | | } |
| | 816 | | else |
| 0 | 817 | | { |
| 0 | 818 | | return "private"; |
| | 819 | | } |
| 3 | 820 | | } |
| | 821 | |
|
| | 822 | | /// <summary> |
| | 823 | | /// Gets a string representation of a type's accessibility |
| | 824 | | /// </summary> |
| | 825 | | /// <param name="type">Type to get accessibility for</param> |
| | 826 | | /// <returns>Accessibility string</returns> |
| | 827 | | private string GetTypeAccessibilityString(Type type) |
| 0 | 828 | | { |
| 0 | 829 | | if (type.IsNestedPublic || type.IsPublic) |
| 0 | 830 | | { |
| 0 | 831 | | return "public"; |
| | 832 | | } |
| 0 | 833 | | else if (type.IsNestedFamily) |
| 0 | 834 | | { |
| 0 | 835 | | return "protected"; |
| | 836 | | } |
| 0 | 837 | | else if (type.IsNestedFamORAssem) |
| 0 | 838 | | { |
| 0 | 839 | | return "protected internal"; |
| | 840 | | } |
| 0 | 841 | | else if (type.IsNestedFamANDAssem) |
| 0 | 842 | | { |
| 0 | 843 | | return "private protected"; |
| | 844 | | } |
| 0 | 845 | | else if (type.IsNestedAssembly || type.IsNotPublic) |
| 0 | 846 | | { |
| 0 | 847 | | return "internal"; |
| | 848 | | } |
| | 849 | | else |
| 0 | 850 | | { |
| 0 | 851 | | return "private"; |
| | 852 | | } |
| 0 | 853 | | } |
| | 854 | |
|
| | 855 | | /// <summary> |
| | 856 | | /// Gets the most accessible of two accessibility strings |
| | 857 | | /// </summary> |
| | 858 | | /// <param name="access1">First accessibility string</param> |
| | 859 | | /// <param name="access2">Second accessibility string</param> |
| | 860 | | /// <returns>Most accessible string</returns> |
| | 861 | | private string GetMostAccessible(string access1, string access2) |
| 1 | 862 | | { |
| 1 | 863 | | var accessRank = new Dictionary<string, int> |
| 1 | 864 | | { |
| 1 | 865 | | { "public", 5 }, |
| 1 | 866 | | { "protected internal", 4 }, |
| 1 | 867 | | { "internal", 3 }, |
| 1 | 868 | | { "protected", 2 }, |
| 1 | 869 | | { "private protected", 1 }, |
| 1 | 870 | | { "private", 0 } |
| 1 | 871 | | }; |
| | 872 | |
|
| 1 | 873 | | if (accessRank.TryGetValue(access1, out int rank1) && accessRank.TryGetValue(access2, out int rank2)) |
| 1 | 874 | | { |
| 1 | 875 | | return rank1 >= rank2 ? access1 : access2; |
| | 876 | | } |
| | 877 | |
|
| 0 | 878 | | return access1; // Default to first if not found |
| 1 | 879 | | } |
| | 880 | | } |