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