| | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | 2 | | using System.Reflection; |
| | 3 | | using DotNetApiDiff.Interfaces; |
| | 4 | | using DotNetApiDiff.Models; |
| | 5 | | using Microsoft.Extensions.Logging; |
| | 6 | |
|
| | 7 | | namespace DotNetApiDiff.ApiExtraction; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Analyzes .NET types and their members to extract API information |
| | 11 | | /// </summary> |
| | 12 | | public class TypeAnalyzer : ITypeAnalyzer |
| | 13 | | { |
| | 14 | | private readonly IMemberSignatureBuilder _signatureBuilder; |
| | 15 | | private readonly ILogger<TypeAnalyzer> _logger; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Creates a new instance of the TypeAnalyzer |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="signatureBuilder">Signature builder for creating normalized signatures</param> |
| | 21 | | /// <param name="logger">Logger for diagnostic information</param> |
| 16 | 22 | | public TypeAnalyzer(IMemberSignatureBuilder signatureBuilder, ILogger<TypeAnalyzer> logger) |
| 16 | 23 | | { |
| 16 | 24 | | _signatureBuilder = signatureBuilder ?? throw new ArgumentNullException(nameof(signatureBuilder)); |
| 16 | 25 | | _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 16 | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Analyzes a type and returns its API member representation |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="type">Type to analyze</param> |
| | 32 | | /// <returns>ApiMember representing the type</returns> |
| | 33 | | public ApiMember AnalyzeType(Type type) |
| 3 | 34 | | { |
| 3 | 35 | | if (type == null) |
| 1 | 36 | | { |
| 1 | 37 | | throw new ArgumentNullException(nameof(type)); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | try |
| 2 | 41 | | { |
| 2 | 42 | | var member = new ApiMember |
| 2 | 43 | | { |
| 2 | 44 | | Name = type.Name, |
| 2 | 45 | | FullName = type.FullName ?? type.Name, |
| 2 | 46 | | Namespace = type.Namespace ?? string.Empty, |
| 2 | 47 | | Signature = _signatureBuilder.BuildTypeSignature(type), |
| 2 | 48 | | Attributes = GetTypeAttributes(type), |
| 2 | 49 | | Type = GetMemberType(type), |
| 2 | 50 | | Accessibility = GetTypeAccessibility(type) |
| 2 | 51 | | }; |
| | 52 | |
|
| 2 | 53 | | return member; |
| | 54 | | } |
| 0 | 55 | | catch (Exception ex) |
| 0 | 56 | | { |
| 0 | 57 | | _logger.LogError(ex, "Error analyzing type {TypeName}", type.Name); |
| 0 | 58 | | throw; |
| | 59 | | } |
| 2 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Analyzes all methods of a type |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="type">Type to analyze methods for</param> |
| | 66 | | /// <returns>Collection of method API members</returns> |
| | 67 | | public IEnumerable<ApiMember> AnalyzeMethods(Type type) |
| 1 | 68 | | { |
| 1 | 69 | | if (type == null) |
| 0 | 70 | | { |
| 0 | 71 | | throw new ArgumentNullException(nameof(type)); |
| | 72 | | } |
| | 73 | |
|
| | 74 | | try |
| 1 | 75 | | { |
| 1 | 76 | | var methods = new List<ApiMember>(); |
| | 77 | |
|
| | 78 | | // Get all methods, excluding property accessors, event accessors, and constructors |
| 1 | 79 | | var methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | |
| 1 | 80 | | BindingFlags.Instance | BindingFlags.Static | |
| 1 | 81 | | BindingFlags.DeclaredOnly) |
| 10 | 82 | | .Where(m => !m.IsSpecialName); |
| | 83 | |
|
| 9 | 84 | | foreach (var method in methodInfos) |
| 3 | 85 | | { |
| | 86 | | // Skip non-public methods unless they're overrides of public methods |
| 3 | 87 | | if (!IsPublicOrOverride(method)) |
| 1 | 88 | | { |
| 1 | 89 | | continue; |
| | 90 | | } |
| | 91 | |
|
| 2 | 92 | | var member = new ApiMember |
| 2 | 93 | | { |
| 2 | 94 | | Name = method.Name, |
| 2 | 95 | | FullName = $"{type.FullName}.{method.Name}", |
| 2 | 96 | | Namespace = type.Namespace ?? string.Empty, |
| 2 | 97 | | DeclaringType = type.FullName ?? type.Name, |
| 2 | 98 | | Signature = _signatureBuilder.BuildMethodSignature(method), |
| 2 | 99 | | Attributes = GetMemberAttributes(method), |
| 2 | 100 | | Type = MemberType.Method, |
| 2 | 101 | | Accessibility = GetMethodAccessibility(method) |
| 2 | 102 | | }; |
| | 103 | |
|
| 2 | 104 | | methods.Add(member); |
| 2 | 105 | | } |
| | 106 | |
|
| 1 | 107 | | return methods; |
| | 108 | | } |
| 0 | 109 | | catch (Exception ex) |
| 0 | 110 | | { |
| 0 | 111 | | _logger.LogError(ex, "Error analyzing methods for type {TypeName}", type.Name); |
| 0 | 112 | | return Enumerable.Empty<ApiMember>(); |
| | 113 | | } |
| 1 | 114 | | } |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Analyzes all properties of a type |
| | 118 | | /// </summary> |
| | 119 | | /// <param name="type">Type to analyze properties for</param> |
| | 120 | | /// <returns>Collection of property API members</returns> |
| | 121 | | public IEnumerable<ApiMember> AnalyzeProperties(Type type) |
| 1 | 122 | | { |
| 1 | 123 | | if (type == null) |
| 0 | 124 | | { |
| 0 | 125 | | throw new ArgumentNullException(nameof(type)); |
| | 126 | | } |
| | 127 | |
|
| | 128 | | try |
| 1 | 129 | | { |
| 1 | 130 | | var properties = new List<ApiMember>(); |
| | 131 | |
|
| 1 | 132 | | var propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | |
| 1 | 133 | | BindingFlags.Instance | BindingFlags.Static | |
| 1 | 134 | | BindingFlags.DeclaredOnly); |
| | 135 | |
|
| 7 | 136 | | foreach (var property in propertyInfos) |
| 2 | 137 | | { |
| | 138 | | // Skip non-public properties unless they're overrides of public properties |
| 2 | 139 | | if (!IsPublicOrOverrideProperty(property)) |
| 1 | 140 | | { |
| 1 | 141 | | continue; |
| | 142 | | } |
| | 143 | |
|
| 1 | 144 | | var member = new ApiMember |
| 1 | 145 | | { |
| 1 | 146 | | Name = property.Name, |
| 1 | 147 | | FullName = $"{type.FullName}.{property.Name}", |
| 1 | 148 | | Namespace = type.Namespace ?? string.Empty, |
| 1 | 149 | | DeclaringType = type.FullName ?? type.Name, |
| 1 | 150 | | Signature = _signatureBuilder.BuildPropertySignature(property), |
| 1 | 151 | | Attributes = GetMemberAttributes(property), |
| 1 | 152 | | Type = MemberType.Property, |
| 1 | 153 | | Accessibility = GetPropertyAccessibility(property) |
| 1 | 154 | | }; |
| | 155 | |
|
| 1 | 156 | | properties.Add(member); |
| 1 | 157 | | } |
| | 158 | |
|
| 1 | 159 | | return properties; |
| | 160 | | } |
| 0 | 161 | | catch (Exception ex) |
| 0 | 162 | | { |
| 0 | 163 | | _logger.LogError(ex, "Error analyzing properties for type {TypeName}", type.Name); |
| 0 | 164 | | return Enumerable.Empty<ApiMember>(); |
| | 165 | | } |
| 1 | 166 | | } |
| | 167 | |
|
| | 168 | | /// <summary> |
| | 169 | | /// Analyzes all fields of a type |
| | 170 | | /// </summary> |
| | 171 | | /// <param name="type">Type to analyze fields for</param> |
| | 172 | | /// <returns>Collection of field API members</returns> |
| | 173 | | public IEnumerable<ApiMember> AnalyzeFields(Type type) |
| 1 | 174 | | { |
| 1 | 175 | | if (type == null) |
| 0 | 176 | | { |
| 0 | 177 | | throw new ArgumentNullException(nameof(type)); |
| | 178 | | } |
| | 179 | |
|
| | 180 | | try |
| 1 | 181 | | { |
| 1 | 182 | | var fields = new List<ApiMember>(); |
| | 183 | |
|
| 1 | 184 | | var fieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | |
| 1 | 185 | | BindingFlags.Instance | BindingFlags.Static | |
| 1 | 186 | | BindingFlags.DeclaredOnly); |
| | 187 | |
|
| 13 | 188 | | foreach (var field in fieldInfos) |
| 5 | 189 | | { |
| | 190 | | // Skip non-public fields and compiler-generated backing fields |
| 5 | 191 | | if (!field.IsPublic || field.Name.StartsWith("<")) |
| 4 | 192 | | { |
| 4 | 193 | | continue; |
| | 194 | | } |
| | 195 | |
|
| 1 | 196 | | var member = new ApiMember |
| 1 | 197 | | { |
| 1 | 198 | | Name = field.Name, |
| 1 | 199 | | FullName = $"{type.FullName}.{field.Name}", |
| 1 | 200 | | Namespace = type.Namespace ?? string.Empty, |
| 1 | 201 | | DeclaringType = type.FullName ?? type.Name, |
| 1 | 202 | | Signature = _signatureBuilder.BuildFieldSignature(field), |
| 1 | 203 | | Attributes = GetMemberAttributes(field), |
| 1 | 204 | | Type = MemberType.Field, |
| 1 | 205 | | Accessibility = GetFieldAccessibility(field) |
| 1 | 206 | | }; |
| | 207 | |
|
| 1 | 208 | | fields.Add(member); |
| 1 | 209 | | } |
| | 210 | |
|
| 1 | 211 | | return fields; |
| | 212 | | } |
| 0 | 213 | | catch (Exception ex) |
| 0 | 214 | | { |
| 0 | 215 | | _logger.LogError(ex, "Error analyzing fields for type {TypeName}", type.Name); |
| 0 | 216 | | return Enumerable.Empty<ApiMember>(); |
| | 217 | | } |
| 1 | 218 | | } |
| | 219 | |
|
| | 220 | | /// <summary> |
| | 221 | | /// Analyzes all events of a type |
| | 222 | | /// </summary> |
| | 223 | | /// <param name="type">Type to analyze events for</param> |
| | 224 | | /// <returns>Collection of event API members</returns> |
| | 225 | | public IEnumerable<ApiMember> AnalyzeEvents(Type type) |
| 1 | 226 | | { |
| 1 | 227 | | if (type == null) |
| 0 | 228 | | { |
| 0 | 229 | | throw new ArgumentNullException(nameof(type)); |
| | 230 | | } |
| | 231 | |
|
| | 232 | | try |
| 1 | 233 | | { |
| 1 | 234 | | var events = new List<ApiMember>(); |
| | 235 | |
|
| 1 | 236 | | var eventInfos = type.GetEvents(BindingFlags.Public | BindingFlags.NonPublic | |
| 1 | 237 | | BindingFlags.Instance | BindingFlags.Static | |
| 1 | 238 | | BindingFlags.DeclaredOnly); |
| | 239 | |
|
| 5 | 240 | | foreach (var eventInfo in eventInfos) |
| 1 | 241 | | { |
| | 242 | | // Skip non-public events unless they're overrides of public events |
| 1 | 243 | | if (!IsPublicOrOverrideEvent(eventInfo)) |
| 0 | 244 | | { |
| 0 | 245 | | continue; |
| | 246 | | } |
| | 247 | |
|
| 1 | 248 | | var member = new ApiMember |
| 1 | 249 | | { |
| 1 | 250 | | Name = eventInfo.Name, |
| 1 | 251 | | FullName = $"{type.FullName}.{eventInfo.Name}", |
| 1 | 252 | | Namespace = type.Namespace ?? string.Empty, |
| 1 | 253 | | DeclaringType = type.FullName ?? type.Name, |
| 1 | 254 | | Signature = _signatureBuilder.BuildEventSignature(eventInfo), |
| 1 | 255 | | Attributes = GetMemberAttributes(eventInfo), |
| 1 | 256 | | Type = MemberType.Event, |
| 1 | 257 | | Accessibility = GetEventAccessibility(eventInfo) |
| 1 | 258 | | }; |
| | 259 | |
|
| 1 | 260 | | events.Add(member); |
| 1 | 261 | | } |
| | 262 | |
|
| 1 | 263 | | return events; |
| | 264 | | } |
| 0 | 265 | | catch (Exception ex) |
| 0 | 266 | | { |
| 0 | 267 | | _logger.LogError(ex, "Error analyzing events for type {TypeName}", type.Name); |
| 0 | 268 | | return Enumerable.Empty<ApiMember>(); |
| | 269 | | } |
| 1 | 270 | | } |
| | 271 | |
|
| | 272 | | /// <summary> |
| | 273 | | /// Analyzes all constructors of a type |
| | 274 | | /// </summary> |
| | 275 | | /// <param name="type">Type to analyze constructors for</param> |
| | 276 | | /// <returns>Collection of constructor API members</returns> |
| | 277 | | public IEnumerable<ApiMember> AnalyzeConstructors(Type type) |
| 1 | 278 | | { |
| 1 | 279 | | if (type == null) |
| 0 | 280 | | { |
| 0 | 281 | | throw new ArgumentNullException(nameof(type)); |
| | 282 | | } |
| | 283 | |
|
| | 284 | | try |
| 1 | 285 | | { |
| 1 | 286 | | var constructors = new List<ApiMember>(); |
| | 287 | |
|
| 1 | 288 | | var constructorInfos = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | |
| 1 | 289 | | BindingFlags.Instance | BindingFlags.Static | |
| 1 | 290 | | BindingFlags.DeclaredOnly); |
| | 291 | |
|
| 7 | 292 | | foreach (var constructor in constructorInfos) |
| 2 | 293 | | { |
| | 294 | | // Skip non-public constructors |
| 2 | 295 | | if (!constructor.IsPublic && !constructor.IsFamily) |
| 1 | 296 | | { |
| 1 | 297 | | continue; |
| | 298 | | } |
| | 299 | |
|
| 1 | 300 | | var member = new ApiMember |
| 1 | 301 | | { |
| 1 | 302 | | Name = constructor.Name, |
| 1 | 303 | | FullName = $"{type.FullName}.{constructor.Name}", |
| 1 | 304 | | Namespace = type.Namespace ?? string.Empty, |
| 1 | 305 | | DeclaringType = type.FullName ?? type.Name, |
| 1 | 306 | | Signature = _signatureBuilder.BuildConstructorSignature(constructor), |
| 1 | 307 | | Attributes = GetMemberAttributes(constructor), |
| 1 | 308 | | Type = MemberType.Constructor, |
| 1 | 309 | | Accessibility = GetMethodAccessibility(constructor) |
| 1 | 310 | | }; |
| | 311 | |
|
| 1 | 312 | | constructors.Add(member); |
| 1 | 313 | | } |
| | 314 | |
|
| 1 | 315 | | return constructors; |
| | 316 | | } |
| 0 | 317 | | catch (Exception ex) |
| 0 | 318 | | { |
| 0 | 319 | | _logger.LogError(ex, "Error analyzing constructors for type {TypeName}", type.Name); |
| 0 | 320 | | return Enumerable.Empty<ApiMember>(); |
| | 321 | | } |
| 1 | 322 | | } |
| | 323 | |
|
| | 324 | | /// <summary> |
| | 325 | | /// Gets the member type for a .NET type |
| | 326 | | /// </summary> |
| | 327 | | /// <param name="type">Type to get member type for</param> |
| | 328 | | /// <returns>Member type</returns> |
| | 329 | | private MemberType GetMemberType(Type type) |
| 2 | 330 | | { |
| 2 | 331 | | if (type.IsInterface) |
| 1 | 332 | | { |
| 1 | 333 | | return MemberType.Interface; |
| | 334 | | } |
| 1 | 335 | | else if (type.IsEnum) |
| 0 | 336 | | { |
| 0 | 337 | | return MemberType.Enum; |
| | 338 | | } |
| 1 | 339 | | else if (type.IsValueType) |
| 0 | 340 | | { |
| 0 | 341 | | return MemberType.Struct; |
| | 342 | | } |
| 1 | 343 | | else if (type.IsSubclassOf(typeof(MulticastDelegate))) |
| 0 | 344 | | { |
| 0 | 345 | | return MemberType.Delegate; |
| | 346 | | } |
| | 347 | | else |
| 1 | 348 | | { |
| 1 | 349 | | return MemberType.Class; |
| | 350 | | } |
| 2 | 351 | | } |
| | 352 | |
|
| | 353 | | /// <summary> |
| | 354 | | /// Gets the accessibility level for a type |
| | 355 | | /// </summary> |
| | 356 | | /// <param name="type">Type to get accessibility for</param> |
| | 357 | | /// <returns>Accessibility level</returns> |
| | 358 | | private AccessibilityLevel GetTypeAccessibility(Type type) |
| 2 | 359 | | { |
| 2 | 360 | | if (type.IsNestedPublic || type.IsPublic) |
| 2 | 361 | | { |
| 2 | 362 | | return AccessibilityLevel.Public; |
| | 363 | | } |
| 0 | 364 | | else if (type.IsNestedFamily) |
| 0 | 365 | | { |
| 0 | 366 | | return AccessibilityLevel.Protected; |
| | 367 | | } |
| 0 | 368 | | else if (type.IsNestedFamORAssem) |
| 0 | 369 | | { |
| 0 | 370 | | return AccessibilityLevel.ProtectedInternal; |
| | 371 | | } |
| 0 | 372 | | else if (type.IsNestedFamANDAssem) |
| 0 | 373 | | { |
| 0 | 374 | | return AccessibilityLevel.ProtectedPrivate; |
| | 375 | | } |
| 0 | 376 | | else if (type.IsNestedAssembly || type.IsNotPublic) |
| 0 | 377 | | { |
| 0 | 378 | | return AccessibilityLevel.Internal; |
| | 379 | | } |
| | 380 | | else |
| 0 | 381 | | { |
| 0 | 382 | | return AccessibilityLevel.Private; |
| | 383 | | } |
| 2 | 384 | | } |
| | 385 | |
|
| | 386 | | /// <summary> |
| | 387 | | /// Gets the accessibility level for a method or constructor |
| | 388 | | /// </summary> |
| | 389 | | /// <param name="method">Method to get accessibility for</param> |
| | 390 | | /// <returns>Accessibility level</returns> |
| | 391 | | private AccessibilityLevel GetMethodAccessibility(MethodBase method) |
| 6 | 392 | | { |
| 6 | 393 | | if (method.IsPublic) |
| 6 | 394 | | { |
| 6 | 395 | | return AccessibilityLevel.Public; |
| | 396 | | } |
| 0 | 397 | | else if (method.IsFamily) |
| 0 | 398 | | { |
| 0 | 399 | | return AccessibilityLevel.Protected; |
| | 400 | | } |
| 0 | 401 | | else if (method.IsFamilyOrAssembly) |
| 0 | 402 | | { |
| 0 | 403 | | return AccessibilityLevel.ProtectedInternal; |
| | 404 | | } |
| 0 | 405 | | else if (method.IsFamilyAndAssembly) |
| 0 | 406 | | { |
| 0 | 407 | | return AccessibilityLevel.ProtectedPrivate; |
| | 408 | | } |
| 0 | 409 | | else if (method.IsAssembly) |
| 0 | 410 | | { |
| 0 | 411 | | return AccessibilityLevel.Internal; |
| | 412 | | } |
| | 413 | | else |
| 0 | 414 | | { |
| 0 | 415 | | return AccessibilityLevel.Private; |
| | 416 | | } |
| 6 | 417 | | } |
| | 418 | |
|
| | 419 | | /// <summary> |
| | 420 | | /// Gets the accessibility level for a property |
| | 421 | | /// </summary> |
| | 422 | | /// <param name="property">Property to get accessibility for</param> |
| | 423 | | /// <returns>Accessibility level</returns> |
| | 424 | | private AccessibilityLevel GetPropertyAccessibility(PropertyInfo property) |
| 1 | 425 | | { |
| 1 | 426 | | var getMethod = property.GetMethod; |
| 1 | 427 | | var setMethod = property.SetMethod; |
| | 428 | |
|
| | 429 | | // Use the most accessible between get and set |
| 1 | 430 | | if (getMethod != null && setMethod != null) |
| 1 | 431 | | { |
| 1 | 432 | | var getAccess = GetMethodAccessibility(getMethod); |
| 1 | 433 | | var setAccess = GetMethodAccessibility(setMethod); |
| 1 | 434 | | return GetMostAccessible(getAccess, setAccess); |
| | 435 | | } |
| 0 | 436 | | else if (getMethod != null) |
| 0 | 437 | | { |
| 0 | 438 | | return GetMethodAccessibility(getMethod); |
| | 439 | | } |
| 0 | 440 | | else if (setMethod != null) |
| 0 | 441 | | { |
| 0 | 442 | | return GetMethodAccessibility(setMethod); |
| | 443 | | } |
| | 444 | |
|
| 0 | 445 | | return AccessibilityLevel.Private; |
| 1 | 446 | | } |
| | 447 | |
|
| | 448 | | /// <summary> |
| | 449 | | /// Gets the accessibility level for a field |
| | 450 | | /// </summary> |
| | 451 | | /// <param name="field">Field to get accessibility for</param> |
| | 452 | | /// <returns>Accessibility level</returns> |
| | 453 | | private AccessibilityLevel GetFieldAccessibility(FieldInfo field) |
| 1 | 454 | | { |
| 1 | 455 | | if (field.IsPublic) |
| 1 | 456 | | { |
| 1 | 457 | | return AccessibilityLevel.Public; |
| | 458 | | } |
| 0 | 459 | | else if (field.IsFamily) |
| 0 | 460 | | { |
| 0 | 461 | | return AccessibilityLevel.Protected; |
| | 462 | | } |
| 0 | 463 | | else if (field.IsFamilyOrAssembly) |
| 0 | 464 | | { |
| 0 | 465 | | return AccessibilityLevel.ProtectedInternal; |
| | 466 | | } |
| 0 | 467 | | else if (field.IsFamilyAndAssembly) |
| 0 | 468 | | { |
| 0 | 469 | | return AccessibilityLevel.ProtectedPrivate; |
| | 470 | | } |
| 0 | 471 | | else if (field.IsAssembly) |
| 0 | 472 | | { |
| 0 | 473 | | return AccessibilityLevel.Internal; |
| | 474 | | } |
| | 475 | | else |
| 0 | 476 | | { |
| 0 | 477 | | return AccessibilityLevel.Private; |
| | 478 | | } |
| 1 | 479 | | } |
| | 480 | |
|
| | 481 | | /// <summary> |
| | 482 | | /// Gets the accessibility level for an event |
| | 483 | | /// </summary> |
| | 484 | | /// <param name="eventInfo">Event to get accessibility for</param> |
| | 485 | | /// <returns>Accessibility level</returns> |
| | 486 | | private AccessibilityLevel GetEventAccessibility(EventInfo eventInfo) |
| 1 | 487 | | { |
| 1 | 488 | | var addMethod = eventInfo.AddMethod; |
| | 489 | |
|
| 1 | 490 | | if (addMethod != null) |
| 1 | 491 | | { |
| 1 | 492 | | return GetMethodAccessibility(addMethod); |
| | 493 | | } |
| | 494 | |
|
| 0 | 495 | | return AccessibilityLevel.Private; |
| 1 | 496 | | } |
| | 497 | |
|
| | 498 | | /// <summary> |
| | 499 | | /// Gets the most accessible of two accessibility levels |
| | 500 | | /// </summary> |
| | 501 | | /// <param name="access1">First accessibility level</param> |
| | 502 | | /// <param name="access2">Second accessibility level</param> |
| | 503 | | /// <returns>Most accessible level</returns> |
| | 504 | | private AccessibilityLevel GetMostAccessible(AccessibilityLevel access1, AccessibilityLevel access2) |
| 1 | 505 | | { |
| 1 | 506 | | return CompareAccessibilityLevels(access1, access2); |
| 1 | 507 | | } |
| | 508 | |
|
| | 509 | | /// <summary> |
| | 510 | | /// Compares two accessibility levels and returns the more accessible one |
| | 511 | | /// </summary> |
| | 512 | | /// <param name="access1">First accessibility level</param> |
| | 513 | | /// <param name="access2">Second accessibility level</param> |
| | 514 | | /// <returns>Most accessible level</returns> |
| | 515 | | private AccessibilityLevel CompareAccessibilityLevels(AccessibilityLevel access1, AccessibilityLevel access2) |
| 1 | 516 | | { |
| 1 | 517 | | if (access1 == AccessibilityLevel.Public || access2 == AccessibilityLevel.Public) |
| 1 | 518 | | { |
| 1 | 519 | | return AccessibilityLevel.Public; |
| | 520 | | } |
| | 521 | |
|
| 0 | 522 | | if (access1 == AccessibilityLevel.Protected || access2 == AccessibilityLevel.Protected) |
| 0 | 523 | | { |
| 0 | 524 | | return AccessibilityLevel.Protected; |
| | 525 | | } |
| | 526 | |
|
| 0 | 527 | | if (access1 == AccessibilityLevel.Internal || access2 == AccessibilityLevel.Internal) |
| 0 | 528 | | { |
| 0 | 529 | | return AccessibilityLevel.Internal; |
| | 530 | | } |
| | 531 | |
|
| 0 | 532 | | return AccessibilityLevel.Private; |
| 1 | 533 | | } |
| | 534 | |
|
| | 535 | | /// <summary> |
| | 536 | | /// Gets the attributes applied to a type |
| | 537 | | /// </summary> |
| | 538 | | /// <param name="type">Type to get attributes for</param> |
| | 539 | | /// <returns>List of attribute names</returns> |
| | 540 | | private List<string> GetTypeAttributes(Type type) |
| 2 | 541 | | { |
| | 542 | | try |
| 2 | 543 | | { |
| 2 | 544 | | return type.GetCustomAttributesData() |
| 1 | 545 | | .Where(a => !IsCompilerGeneratedAttribute(a)) |
| 1 | 546 | | .Select(a => a.AttributeType.Name) |
| 2 | 547 | | .ToList(); |
| | 548 | | } |
| 0 | 549 | | catch (Exception ex) |
| 0 | 550 | | { |
| 0 | 551 | | _logger.LogWarning(ex, "Error getting attributes for type {TypeName}", type.Name); |
| 0 | 552 | | return new List<string>(); |
| | 553 | | } |
| 2 | 554 | | } |
| | 555 | |
|
| | 556 | | /// <summary> |
| | 557 | | /// Gets the attributes applied to a member |
| | 558 | | /// </summary> |
| | 559 | | /// <param name="member">Member to get attributes for</param> |
| | 560 | | /// <returns>List of attribute names</returns> |
| | 561 | | private List<string> GetMemberAttributes(MemberInfo member) |
| 6 | 562 | | { |
| | 563 | | try |
| 6 | 564 | | { |
| 6 | 565 | | return member.GetCustomAttributesData() |
| 0 | 566 | | .Where(a => !IsCompilerGeneratedAttribute(a)) |
| 0 | 567 | | .Select(a => a.AttributeType.Name) |
| 6 | 568 | | .ToList(); |
| | 569 | | } |
| 0 | 570 | | catch (Exception ex) |
| 0 | 571 | | { |
| 0 | 572 | | _logger.LogWarning(ex, "Error getting attributes for member {MemberName}", member.Name); |
| 0 | 573 | | return new List<string>(); |
| | 574 | | } |
| 6 | 575 | | } |
| | 576 | |
|
| | 577 | | /// <summary> |
| | 578 | | /// Checks if an attribute is compiler-generated |
| | 579 | | /// </summary> |
| | 580 | | /// <param name="attribute">Attribute to check</param> |
| | 581 | | /// <returns>True if compiler-generated, false otherwise</returns> |
| | 582 | | private bool IsCompilerGeneratedAttribute(object attribute) |
| 0 | 583 | | { |
| 0 | 584 | | var typeName = attribute.GetType().Name; |
| 0 | 585 | | return typeName == "CompilerGeneratedAttribute" || |
| 0 | 586 | | typeName == "DebuggerHiddenAttribute" || |
| 0 | 587 | | typeName == "DebuggerNonUserCodeAttribute"; |
| 0 | 588 | | } |
| | 589 | |
|
| | 590 | | /// <summary> |
| | 591 | | /// Checks if an attribute is compiler-generated |
| | 592 | | /// </summary> |
| | 593 | | /// <param name="attributeData">Attribute data to check</param> |
| | 594 | | /// <returns>True if compiler-generated, false otherwise</returns> |
| | 595 | | private bool IsCompilerGeneratedAttribute(CustomAttributeData attributeData) |
| 1 | 596 | | { |
| 1 | 597 | | var typeName = attributeData.AttributeType.Name; |
| 1 | 598 | | return typeName == "CompilerGeneratedAttribute" || |
| 1 | 599 | | typeName == "DebuggerHiddenAttribute" || |
| 1 | 600 | | typeName == "DebuggerNonUserCodeAttribute"; |
| 1 | 601 | | } |
| | 602 | |
|
| | 603 | | /// <summary> |
| | 604 | | /// Checks if a method is public or an override of a public method |
| | 605 | | /// </summary> |
| | 606 | | /// <param name="method">Method to check</param> |
| | 607 | | /// <returns>True if public or override, false otherwise</returns> |
| | 608 | | private bool IsPublicOrOverride(MethodInfo method) |
| 5 | 609 | | { |
| | 610 | | // If the method is public, include it |
| 5 | 611 | | if (method.IsPublic) |
| 2 | 612 | | { |
| 2 | 613 | | return true; |
| | 614 | | } |
| | 615 | |
|
| | 616 | | // If the method is an override, check if it's overriding a public method |
| 3 | 617 | | if (method.GetBaseDefinition() != method) |
| 0 | 618 | | { |
| 0 | 619 | | var baseMethod = method.GetBaseDefinition(); |
| 0 | 620 | | if (baseMethod != null && baseMethod.IsPublic) |
| 0 | 621 | | { |
| 0 | 622 | | return true; |
| | 623 | | } |
| 0 | 624 | | } |
| | 625 | |
|
| 3 | 626 | | return false; |
| 5 | 627 | | } |
| | 628 | |
|
| | 629 | | /// <summary> |
| | 630 | | /// Checks if a property is public or an override of a public property |
| | 631 | | /// </summary> |
| | 632 | | /// <param name="property">Property to check</param> |
| | 633 | | /// <returns>True if public or override, false otherwise</returns> |
| | 634 | | private bool IsPublicOrOverrideProperty(PropertyInfo property) |
| 2 | 635 | | { |
| 2 | 636 | | var getMethod = property.GetMethod; |
| 2 | 637 | | var setMethod = property.SetMethod; |
| | 638 | |
|
| | 639 | | // If either accessor is public, include the property |
| 2 | 640 | | if ((getMethod != null && getMethod.IsPublic) || (setMethod != null && setMethod.IsPublic)) |
| 1 | 641 | | { |
| 1 | 642 | | return true; |
| | 643 | | } |
| | 644 | |
|
| | 645 | | // Check if either accessor is overriding a public accessor |
| 1 | 646 | | if ((getMethod != null && IsPublicOrOverride(getMethod)) || |
| 1 | 647 | | (setMethod != null && IsPublicOrOverride(setMethod))) |
| 0 | 648 | | { |
| 0 | 649 | | return true; |
| | 650 | | } |
| | 651 | |
|
| 1 | 652 | | return false; |
| 2 | 653 | | } |
| | 654 | |
|
| | 655 | | /// <summary> |
| | 656 | | /// Checks if an event is public or an override of a public event |
| | 657 | | /// </summary> |
| | 658 | | /// <param name="eventInfo">Event to check</param> |
| | 659 | | /// <returns>True if public or override, false otherwise</returns> |
| | 660 | | private bool IsPublicOrOverrideEvent(EventInfo eventInfo) |
| 1 | 661 | | { |
| 1 | 662 | | var addMethod = eventInfo.AddMethod; |
| 1 | 663 | | var removeMethod = eventInfo.RemoveMethod; |
| | 664 | |
|
| | 665 | | // If either accessor is public, include the event |
| 1 | 666 | | if ((addMethod != null && addMethod.IsPublic) || (removeMethod != null && removeMethod.IsPublic)) |
| 1 | 667 | | { |
| 1 | 668 | | return true; |
| | 669 | | } |
| | 670 | |
|
| | 671 | | // Check if either accessor is overriding a public accessor |
| 0 | 672 | | if ((addMethod != null && IsPublicOrOverride(addMethod)) || |
| 0 | 673 | | (removeMethod != null && IsPublicOrOverride(removeMethod))) |
| 0 | 674 | | { |
| 0 | 675 | | return true; |
| | 676 | | } |
| | 677 | |
|
| 0 | 678 | | return false; |
| 1 | 679 | | } |
| | 680 | | } |