| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 7 | | using NexusLabs.Needlr.Generators.Models; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.Generators; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Helper for discovering and analyzing options types, including positional |
| | | 13 | | /// records, bindable properties, data annotations, and nested options filtering. |
| | | 14 | | /// </summary> |
| | | 15 | | internal static class OptionsDiscoveryHelper |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Detects whether a type is a positional record that needs a generated parameterless constructor. |
| | | 19 | | /// Returns null if not a positional record, or PositionalRecordInfo if it is. |
| | | 20 | | /// </summary> |
| | | 21 | | internal static PositionalRecordInfo? DetectPositionalRecord( |
| | | 22 | | INamedTypeSymbol typeSymbol, |
| | | 23 | | IReadOnlyList<OptionsPropertyInfo> bindableProperties) |
| | | 24 | | { |
| | | 25 | | // Must be a record |
| | 176 | 26 | | if (!typeSymbol.IsRecord) |
| | 159 | 27 | | return null; |
| | | 28 | | |
| | | 29 | | // Check for primary constructor with parameters |
| | | 30 | | // Records with positional parameters have a primary constructor generated from the record declaration |
| | 17 | 31 | | var primaryCtor = typeSymbol.InstanceConstructors |
| | 37 | 32 | | .FirstOrDefault(c => c.Parameters.Length > 0 && IsPrimaryConstructor(c, typeSymbol)); |
| | | 33 | | |
| | 17 | 34 | | if (primaryCtor == null) |
| | 3 | 35 | | return null; |
| | | 36 | | |
| | | 37 | | // Check if the record has a parameterless constructor already |
| | | 38 | | // (user-defined or from record with init-only properties) |
| | 14 | 39 | | var hasParameterlessCtor = typeSymbol.InstanceConstructors |
| | 42 | 40 | | .Any(c => c.Parameters.Length == 0 && !c.IsImplicitlyDeclared); |
| | | 41 | | |
| | 14 | 42 | | if (hasParameterlessCtor) |
| | 0 | 43 | | return null; // Doesn't need generated constructor |
| | | 44 | | |
| | | 45 | | // Check if partial |
| | 14 | 46 | | var isPartial = typeSymbol.DeclaringSyntaxReferences |
| | 14 | 47 | | .Select(r => r.GetSyntax()) |
| | 14 | 48 | | .OfType<TypeDeclarationSyntax>() |
| | 54 | 49 | | .Any(s => s.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword))); |
| | | 50 | | |
| | | 51 | | // Extract constructor parameters |
| | 14 | 52 | | var parameters = primaryCtor.Parameters |
| | 100 | 53 | | .Select(p => new PositionalRecordParameter( |
| | 100 | 54 | | bindableProperties.First(property => |
| | 742 | 55 | | property.Name.Equals(p.Name, StringComparison.Ordinal) && |
| | 742 | 56 | | property.TypeName == p.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)), |
| | 100 | 57 | | p.Type.IsValueType)) |
| | 14 | 58 | | .ToList(); |
| | | 59 | | |
| | | 60 | | // Get namespace |
| | 14 | 61 | | var containingNamespace = typeSymbol.ContainingNamespace.IsGlobalNamespace |
| | 14 | 62 | | ? "" |
| | 14 | 63 | | : typeSymbol.ContainingNamespace.ToDisplayString(); |
| | | 64 | | |
| | 14 | 65 | | return new PositionalRecordInfo( |
| | 14 | 66 | | typeSymbol.Name, |
| | 14 | 67 | | containingNamespace, |
| | 14 | 68 | | isPartial, |
| | 14 | 69 | | parameters); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Extracts bindable properties from an options type for AOT code generation. |
| | | 74 | | /// </summary> |
| | | 75 | | internal static IReadOnlyList<OptionsPropertyInfo> ExtractBindableProperties(INamedTypeSymbol typeSymbol, HashSet<st |
| | | 76 | | { |
| | 222 | 77 | | var properties = new List<OptionsPropertyInfo>(); |
| | 222 | 78 | | visitedTypes ??= new HashSet<string>(); |
| | | 79 | | |
| | | 80 | | // Prevent infinite recursion for circular references |
| | 222 | 81 | | var typeFullName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 222 | 82 | | if (!visitedTypes.Add(typeFullName)) |
| | | 83 | | { |
| | 6 | 84 | | return properties; // Already visited - circular reference |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | try |
| | | 88 | | { |
| | 4922 | 89 | | foreach (var member in typeSymbol.GetMembers()) |
| | | 90 | | { |
| | 2245 | 91 | | if (member is not IPropertySymbol property) |
| | | 92 | | continue; |
| | | 93 | | |
| | | 94 | | // Skip static, indexers, readonly properties without init |
| | 471 | 95 | | if (property.IsStatic || property.IsIndexer) |
| | | 96 | | continue; |
| | | 97 | | |
| | | 98 | | // Must have a setter (set or init) |
| | 471 | 99 | | if (property.SetMethod == null) |
| | | 100 | | continue; |
| | | 101 | | |
| | | 102 | | // Check if it's init-only |
| | 454 | 103 | | var isInitOnly = property.SetMethod.IsInitOnly; |
| | | 104 | | |
| | | 105 | | // Get nullability info |
| | 454 | 106 | | var isNullable = property.NullableAnnotation == NullableAnnotation.Annotated || |
| | 454 | 107 | | (property.Type is INamedTypeSymbol namedType && |
| | 454 | 108 | | namedType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T); |
| | | 109 | | |
| | 454 | 110 | | var typeName = property.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | | 111 | | |
| | | 112 | | // Check if it's an enum type |
| | 454 | 113 | | var isEnum = false; |
| | 454 | 114 | | string? enumTypeName = null; |
| | 454 | 115 | | var actualType = property.Type; |
| | | 116 | | |
| | | 117 | | // For nullable types, get the underlying type |
| | 454 | 118 | | if (actualType is INamedTypeSymbol nullableType && |
| | 454 | 119 | | nullableType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T && |
| | 454 | 120 | | nullableType.TypeArguments.Length == 1) |
| | | 121 | | { |
| | 29 | 122 | | actualType = nullableType.TypeArguments[0]; |
| | | 123 | | } |
| | | 124 | | |
| | 454 | 125 | | if (actualType.TypeKind == TypeKind.Enum) |
| | | 126 | | { |
| | 35 | 127 | | isEnum = true; |
| | 35 | 128 | | enumTypeName = actualType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | // Detect complex types |
| | 454 | 132 | | var (complexKind, elementTypeName, nestedProps) = AnalyzeComplexType(property.Type, visitedTypes); |
| | | 133 | | |
| | | 134 | | // Extract DataAnnotation attributes |
| | 454 | 135 | | var dataAnnotations = ExtractDataAnnotations(property); |
| | | 136 | | |
| | 454 | 137 | | properties.Add(new OptionsPropertyInfo( |
| | 454 | 138 | | property.Name, |
| | 454 | 139 | | typeName, |
| | 454 | 140 | | isNullable, |
| | 454 | 141 | | isInitOnly, |
| | 454 | 142 | | isEnum, |
| | 454 | 143 | | enumTypeName, |
| | 454 | 144 | | complexKind, |
| | 454 | 145 | | elementTypeName, |
| | 454 | 146 | | nestedProps, |
| | 454 | 147 | | dataAnnotations)); |
| | | 148 | | } |
| | | 149 | | |
| | 216 | 150 | | return properties; |
| | | 151 | | } |
| | | 152 | | finally |
| | | 153 | | { |
| | 216 | 154 | | visitedTypes.Remove(typeFullName); |
| | 216 | 155 | | } |
| | 216 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Extracts DataAnnotation validation attributes from a property symbol. |
| | | 160 | | /// </summary> |
| | | 161 | | internal static IReadOnlyList<DataAnnotationInfo> ExtractDataAnnotations(IPropertySymbol property) |
| | | 162 | | { |
| | 454 | 163 | | var annotations = new List<DataAnnotationInfo>(); |
| | | 164 | | |
| | 954 | 165 | | foreach (var attr in property.GetAttributes()) |
| | | 166 | | { |
| | 23 | 167 | | var attrClass = attr.AttributeClass; |
| | 23 | 168 | | if (attrClass == null) continue; |
| | | 169 | | |
| | | 170 | | // Get the attribute type name - use ContainingNamespace + Name for reliable matching |
| | 23 | 171 | | var attrNamespace = attrClass.ContainingNamespace?.ToDisplayString() ?? ""; |
| | 23 | 172 | | var attrTypeName = attrClass.Name; |
| | | 173 | | |
| | | 174 | | // Only process System.ComponentModel.DataAnnotations attributes |
| | 23 | 175 | | if (attrNamespace != "System.ComponentModel.DataAnnotations") |
| | | 176 | | continue; |
| | | 177 | | |
| | | 178 | | // Extract error message if present |
| | 23 | 179 | | string? errorMessage = null; |
| | 51 | 180 | | foreach (var namedArg in attr.NamedArguments) |
| | | 181 | | { |
| | 3 | 182 | | if (namedArg.Key == "ErrorMessage" && namedArg.Value.Value is string msg) |
| | | 183 | | { |
| | 1 | 184 | | errorMessage = msg; |
| | 1 | 185 | | break; |
| | | 186 | | } |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | // Check for known DataAnnotation attributes |
| | 23 | 190 | | if (attrTypeName == "RequiredAttribute") |
| | | 191 | | { |
| | 12 | 192 | | annotations.Add(new DataAnnotationInfo(DataAnnotationKind.Required, errorMessage)); |
| | | 193 | | } |
| | 11 | 194 | | else if (attrTypeName == "RangeAttribute") |
| | | 195 | | { |
| | 12 | 196 | | object? min = null, max = null; |
| | 6 | 197 | | if (attr.ConstructorArguments.Length >= 2) |
| | | 198 | | { |
| | 6 | 199 | | min = attr.ConstructorArguments[0].Value; |
| | 6 | 200 | | max = attr.ConstructorArguments[1].Value; |
| | | 201 | | } |
| | 6 | 202 | | annotations.Add(new DataAnnotationInfo(DataAnnotationKind.Range, errorMessage, min, max)); |
| | | 203 | | } |
| | 5 | 204 | | else if (attrTypeName == "StringLengthAttribute") |
| | | 205 | | { |
| | 2 | 206 | | object? maxLen = null; |
| | 2 | 207 | | int? minLen = null; |
| | 2 | 208 | | if (attr.ConstructorArguments.Length >= 1) |
| | | 209 | | { |
| | 2 | 210 | | maxLen = attr.ConstructorArguments[0].Value; |
| | | 211 | | } |
| | 8 | 212 | | foreach (var namedArg in attr.NamedArguments) |
| | | 213 | | { |
| | 2 | 214 | | if (namedArg.Key == "MinimumLength" && namedArg.Value.Value is int ml) |
| | | 215 | | { |
| | 2 | 216 | | minLen = ml; |
| | | 217 | | } |
| | | 218 | | } |
| | 2 | 219 | | annotations.Add(new DataAnnotationInfo(DataAnnotationKind.StringLength, errorMessage, null, maxLen, null |
| | | 220 | | } |
| | 3 | 221 | | else if (attrTypeName == "MinLengthAttribute") |
| | | 222 | | { |
| | 1 | 223 | | int? minLen = null; |
| | 1 | 224 | | if (attr.ConstructorArguments.Length >= 1 && attr.ConstructorArguments[0].Value is int ml) |
| | | 225 | | { |
| | 1 | 226 | | minLen = ml; |
| | | 227 | | } |
| | 1 | 228 | | annotations.Add(new DataAnnotationInfo(DataAnnotationKind.MinLength, errorMessage, null, null, null, min |
| | | 229 | | } |
| | 2 | 230 | | else if (attrTypeName == "MaxLengthAttribute") |
| | | 231 | | { |
| | 1 | 232 | | object? maxLen = null; |
| | 1 | 233 | | if (attr.ConstructorArguments.Length >= 1) |
| | | 234 | | { |
| | 1 | 235 | | maxLen = attr.ConstructorArguments[0].Value; |
| | | 236 | | } |
| | 1 | 237 | | annotations.Add(new DataAnnotationInfo(DataAnnotationKind.MaxLength, errorMessage, null, maxLen)); |
| | | 238 | | } |
| | 1 | 239 | | else if (attrTypeName == "RegularExpressionAttribute") |
| | | 240 | | { |
| | 1 | 241 | | string? pattern = null; |
| | 1 | 242 | | if (attr.ConstructorArguments.Length >= 1 && attr.ConstructorArguments[0].Value is string p) |
| | | 243 | | { |
| | 1 | 244 | | pattern = p; |
| | | 245 | | } |
| | 1 | 246 | | annotations.Add(new DataAnnotationInfo(DataAnnotationKind.RegularExpression, errorMessage, null, null, p |
| | | 247 | | } |
| | 0 | 248 | | else if (attrTypeName == "EmailAddressAttribute") |
| | | 249 | | { |
| | 0 | 250 | | annotations.Add(new DataAnnotationInfo(DataAnnotationKind.EmailAddress, errorMessage)); |
| | | 251 | | } |
| | 0 | 252 | | else if (attrTypeName == "PhoneAttribute") |
| | | 253 | | { |
| | 0 | 254 | | annotations.Add(new DataAnnotationInfo(DataAnnotationKind.Phone, errorMessage)); |
| | | 255 | | } |
| | 0 | 256 | | else if (attrTypeName == "UrlAttribute") |
| | | 257 | | { |
| | 0 | 258 | | annotations.Add(new DataAnnotationInfo(DataAnnotationKind.Url, errorMessage)); |
| | | 259 | | } |
| | 0 | 260 | | else if (IsValidationAttribute(attrClass)) |
| | | 261 | | { |
| | | 262 | | // Unsupported validation attribute |
| | 0 | 263 | | annotations.Add(new DataAnnotationInfo(DataAnnotationKind.Unsupported, errorMessage)); |
| | | 264 | | } |
| | | 265 | | } |
| | | 266 | | |
| | 454 | 267 | | return annotations; |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Attempts to extract nested properties from a type if it is a bindable class. |
| | | 272 | | /// </summary> |
| | | 273 | | internal static IReadOnlyList<OptionsPropertyInfo>? TryGetNestedProperties(ITypeSymbol elementType, HashSet<string> |
| | | 274 | | { |
| | 50 | 275 | | if (elementType is INamedTypeSymbol namedElement && IsBindableClass(namedElement)) |
| | | 276 | | { |
| | 15 | 277 | | var props = ExtractBindableProperties(namedElement, visitedTypes); |
| | 15 | 278 | | return props.Count > 0 ? props : null; |
| | | 279 | | } |
| | 35 | 280 | | return null; |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | /// <summary> |
| | | 284 | | /// Filters out nested options types that are used as properties in other options types. |
| | | 285 | | /// These should not be registered separately - they are bound as part of their parent. |
| | | 286 | | /// </summary> |
| | | 287 | | internal static List<DiscoveredOptions> FilterNestedOptions(List<DiscoveredOptions> options, Compilation compilation |
| | | 288 | | { |
| | | 289 | | // Build a set of all options type names |
| | 63 | 290 | | var optionsTypeNames = new HashSet<string>(options.Select(o => o.TypeName)); |
| | | 291 | | |
| | | 292 | | // Find all options types that are used as properties in other options types |
| | 19 | 293 | | var nestedTypeNames = new HashSet<string>(); |
| | | 294 | | |
| | 126 | 295 | | foreach (var opt in options) |
| | | 296 | | { |
| | | 297 | | // Find the type symbol for this options type |
| | 44 | 298 | | var typeSymbol = FindTypeSymbol(compilation, opt.TypeName); |
| | 44 | 299 | | if (typeSymbol == null) |
| | | 300 | | continue; |
| | | 301 | | |
| | | 302 | | // Check all properties of this type |
| | 640 | 303 | | foreach (var member in typeSymbol.GetMembers()) |
| | | 304 | | { |
| | 276 | 305 | | if (member is not IPropertySymbol property) |
| | | 306 | | continue; |
| | | 307 | | |
| | | 308 | | // Skip non-class property types (primitives, structs, etc.) |
| | 56 | 309 | | if (property.Type is not INamedTypeSymbol propertyType) |
| | | 310 | | continue; |
| | | 311 | | |
| | 56 | 312 | | if (propertyType.TypeKind != TypeKind.Class) |
| | | 313 | | continue; |
| | | 314 | | |
| | | 315 | | // Get the fully qualified name of the property type |
| | 42 | 316 | | var propertyTypeName = TypeDiscoveryHelper.GetFullyQualifiedName(propertyType); |
| | | 317 | | |
| | | 318 | | // If this property type is also an [Options] type, mark it as nested |
| | 42 | 319 | | if (optionsTypeNames.Contains(propertyTypeName)) |
| | | 320 | | { |
| | 9 | 321 | | nestedTypeNames.Add(propertyTypeName); |
| | | 322 | | } |
| | | 323 | | } |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | // Return only root options (those not used as properties in other options) |
| | 63 | 327 | | return options.Where(o => !nestedTypeNames.Contains(o.TypeName)).ToList(); |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | private static bool IsPrimaryConstructor(IMethodSymbol ctor, INamedTypeSymbol recordType) |
| | | 331 | | { |
| | | 332 | | // For positional records, the primary constructor parameters correspond to auto-properties |
| | | 333 | | // Check if each parameter has a matching property |
| | 237 | 334 | | foreach (var param in ctor.Parameters) |
| | | 335 | | { |
| | 103 | 336 | | var hasMatchingProperty = recordType.GetMembers() |
| | 103 | 337 | | .OfType<IPropertySymbol>() |
| | 954 | 338 | | .Any(p => p.Name.Equals(param.Name, StringComparison.Ordinal) && |
| | 954 | 339 | | SymbolEqualityComparer.Default.Equals(p.Type, param.Type)); |
| | | 340 | | |
| | 103 | 341 | | if (!hasMatchingProperty) |
| | 3 | 342 | | return false; |
| | | 343 | | } |
| | | 344 | | |
| | 14 | 345 | | return true; |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | private static (ComplexTypeKind Kind, string? ElementTypeName, IReadOnlyList<OptionsPropertyInfo>? NestedProperties) |
| | | 349 | | ITypeSymbol typeSymbol, |
| | | 350 | | HashSet<string> visitedTypes) |
| | | 351 | | { |
| | | 352 | | // Check for array |
| | 454 | 353 | | if (typeSymbol is IArrayTypeSymbol arrayType) |
| | | 354 | | { |
| | 15 | 355 | | var elementType = arrayType.ElementType; |
| | 15 | 356 | | var elementTypeName = elementType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 15 | 357 | | var nestedProps = TryGetNestedProperties(elementType, visitedTypes); |
| | 15 | 358 | | return (ComplexTypeKind.Array, elementTypeName, nestedProps); |
| | | 359 | | } |
| | | 360 | | |
| | 439 | 361 | | if (typeSymbol is not INamedTypeSymbol namedType) |
| | | 362 | | { |
| | 0 | 363 | | return (ComplexTypeKind.None, null, null); |
| | | 364 | | } |
| | | 365 | | |
| | | 366 | | // Check for Dictionary<string, T> |
| | 439 | 367 | | if (IsDictionaryType(namedType)) |
| | | 368 | | { |
| | 16 | 369 | | var valueType = namedType.TypeArguments[1]; |
| | 16 | 370 | | var valueTypeName = valueType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 16 | 371 | | var nestedProps = TryGetNestedProperties(valueType, visitedTypes); |
| | 16 | 372 | | return (ComplexTypeKind.Dictionary, valueTypeName, nestedProps); |
| | | 373 | | } |
| | | 374 | | |
| | | 375 | | // Check for List<T>, IList<T>, ICollection<T>, IEnumerable<T> |
| | 423 | 376 | | if (IsListType(namedType)) |
| | | 377 | | { |
| | 19 | 378 | | var elementType = namedType.TypeArguments[0]; |
| | 19 | 379 | | var elementTypeName = elementType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 19 | 380 | | var nestedProps = TryGetNestedProperties(elementType, visitedTypes); |
| | 19 | 381 | | return (ComplexTypeKind.List, elementTypeName, nestedProps); |
| | | 382 | | } |
| | | 383 | | |
| | | 384 | | // Check for nested object (class with bindable properties) |
| | 404 | 385 | | if (IsBindableClass(namedType)) |
| | | 386 | | { |
| | 31 | 387 | | var nestedProps = ExtractBindableProperties(namedType, visitedTypes); |
| | 31 | 388 | | if (nestedProps.Count > 0) |
| | | 389 | | { |
| | 23 | 390 | | return (ComplexTypeKind.NestedObject, null, nestedProps); |
| | | 391 | | } |
| | | 392 | | } |
| | | 393 | | |
| | 381 | 394 | | return (ComplexTypeKind.None, null, null); |
| | | 395 | | } |
| | | 396 | | |
| | | 397 | | private static bool IsValidationAttribute(INamedTypeSymbol attrClass) |
| | | 398 | | { |
| | | 399 | | // Check if this inherits from ValidationAttribute |
| | 0 | 400 | | var current = attrClass.BaseType; |
| | 0 | 401 | | while (current != null) |
| | | 402 | | { |
| | 0 | 403 | | if (current.ToDisplayString() == "System.ComponentModel.DataAnnotations.ValidationAttribute") |
| | 0 | 404 | | return true; |
| | 0 | 405 | | current = current.BaseType; |
| | | 406 | | } |
| | 0 | 407 | | return false; |
| | | 408 | | } |
| | | 409 | | |
| | | 410 | | private static bool IsDictionaryType(INamedTypeSymbol type) |
| | | 411 | | { |
| | | 412 | | // Check for Dictionary<TKey, TValue> or IDictionary<TKey, TValue> |
| | 439 | 413 | | if (type.TypeArguments.Length != 2) |
| | 423 | 414 | | return false; |
| | | 415 | | |
| | 16 | 416 | | var typeName = type.OriginalDefinition.ToDisplayString(); |
| | 16 | 417 | | return typeName == "System.Collections.Generic.Dictionary<TKey, TValue>" || |
| | 16 | 418 | | typeName == "System.Collections.Generic.IDictionary<TKey, TValue>"; |
| | | 419 | | } |
| | | 420 | | |
| | | 421 | | private static bool IsListType(INamedTypeSymbol type) |
| | | 422 | | { |
| | 423 | 423 | | if (type.TypeArguments.Length != 1) |
| | 375 | 424 | | return false; |
| | | 425 | | |
| | 48 | 426 | | var typeName = type.OriginalDefinition.ToDisplayString(); |
| | 48 | 427 | | return typeName == "System.Collections.Generic.List<T>" || |
| | 48 | 428 | | typeName == "System.Collections.Generic.IList<T>" || |
| | 48 | 429 | | typeName == "System.Collections.Generic.ICollection<T>" || |
| | 48 | 430 | | typeName == "System.Collections.Generic.IEnumerable<T>"; |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | private static bool IsBindableClass(INamedTypeSymbol type) |
| | | 434 | | { |
| | | 435 | | // Must be a class or struct, not abstract, not a system type |
| | 454 | 436 | | if (type.TypeKind != TypeKind.Class && type.TypeKind != TypeKind.Struct) |
| | 37 | 437 | | return false; |
| | | 438 | | |
| | 417 | 439 | | if (type.IsAbstract) |
| | 4 | 440 | | return false; |
| | | 441 | | |
| | | 442 | | // Skip system types and primitives |
| | 413 | 443 | | var ns = type.ContainingNamespace?.ToDisplayString() ?? ""; |
| | 413 | 444 | | if (ns.StartsWith("System")) |
| | | 445 | | { |
| | | 446 | | // Skip known non-bindable System namespaces |
| | 367 | 447 | | if (ns == "System" || ns.StartsWith("System.Collections") || ns.StartsWith("System.Threading")) |
| | 367 | 448 | | return false; |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | // Must have a parameterless constructor (explicit or implicit) |
| | | 452 | | // Note: Classes without any explicit constructors have an implicit parameterless constructor |
| | 92 | 453 | | var hasExplicitConstructors = type.InstanceConstructors.Any(c => !c.IsImplicitlyDeclared); |
| | 46 | 454 | | if (hasExplicitConstructors) |
| | | 455 | | { |
| | 0 | 456 | | var hasParameterlessCtor = type.InstanceConstructors |
| | 0 | 457 | | .Any(c => c.Parameters.Length == 0 && c.DeclaredAccessibility == Accessibility.Public); |
| | 0 | 458 | | return hasParameterlessCtor; |
| | | 459 | | } |
| | | 460 | | |
| | | 461 | | // No explicit constructors means implicit parameterless constructor exists |
| | 46 | 462 | | return true; |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | private static INamedTypeSymbol? FindTypeSymbol(Compilation compilation, string fullyQualifiedName) |
| | | 466 | | { |
| | | 467 | | // Strip global:: prefix if present |
| | 44 | 468 | | var typeName = fullyQualifiedName.StartsWith("global::") |
| | 44 | 469 | | ? fullyQualifiedName.Substring(8) |
| | 44 | 470 | | : fullyQualifiedName; |
| | | 471 | | |
| | 44 | 472 | | return compilation.GetTypeByMetadataName(typeName); |
| | | 473 | | } |
| | | 474 | | } |