|
|
|
|
@ -179,11 +179,13 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
if (!method.ReturnsVoid) |
|
|
|
|
{ |
|
|
|
|
if (method.ReturnType.Name == "Task") |
|
|
|
|
{ |
|
|
|
|
if (method.ReturnType is INamedTypeSymbol { IsGenericType: true } namedTypeSymbol) |
|
|
|
|
{ |
|
|
|
|
namespaces.AddRange(namedTypeSymbol.TypeArguments.Select(GetNamespaceOfType)); |
|
|
|
|
|
|
|
|
|
if (method.ReturnType.Name != "Task") |
|
|
|
|
namespaces.Add(GetNamespaceOfType(method.ReturnType)); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
namespaces.Add(GetNamespaceOfType(method.ReturnType)); |
|
|
|
|
@ -215,11 +217,8 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
|
{ |
|
|
|
|
stringBuilder.Append($"{INDENT}public {method.ReturnType.Name}"); |
|
|
|
|
|
|
|
|
|
if (method.ReturnType.Name == "Task") |
|
|
|
|
{ |
|
|
|
|
if (method.ReturnType is INamedTypeSymbol { IsGenericType: true } namedTypeSymbol) |
|
|
|
|
stringBuilder.Append($"<{string.Join(", ", namedTypeSymbol.TypeArguments.Select(a => a.Name))}>"); |
|
|
|
|
} |
|
|
|
|
if (method.ReturnType is INamedTypeSymbol { IsGenericType: true } namedReturnType) |
|
|
|
|
stringBuilder.Append(GetGenericArguments(namedReturnType)); |
|
|
|
|
|
|
|
|
|
stringBuilder.Append($" {method.Name}"); |
|
|
|
|
|
|
|
|
|
@ -251,14 +250,12 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
|
stringBuilder.AppendLine(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (method.ReturnType.Name == "Task") |
|
|
|
|
{ |
|
|
|
|
stringBuilder.Append($"{INDENT}{INDENT}return container.ResolveAsync"); |
|
|
|
|
|
|
|
|
|
if (method.ReturnType is INamedTypeSymbol { IsGenericType: true } namedTypeSymbol) |
|
|
|
|
stringBuilder.Append($"<{string.Join(", ", namedTypeSymbol.TypeArguments.Select(a => a.Name))}>"); |
|
|
|
|
|
|
|
|
|
stringBuilder.Append("("); |
|
|
|
|
{ |
|
|
|
|
if (method.ReturnType.Name == "Task") |
|
|
|
|
stringBuilder.Append($"{INDENT}{INDENT}return container.ResolveAsync{GetGenericArguments(namedTypeSymbol)}("); |
|
|
|
|
else |
|
|
|
|
stringBuilder.Append($"{INDENT}{INDENT}return container.Resolve<{method.ReturnType.Name}{GetGenericArguments(namedTypeSymbol)}>("); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
stringBuilder.Append($"{INDENT}{INDENT}return container.Resolve<{method.ReturnType.Name}>("); |
|
|
|
|
@ -305,6 +302,9 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
|
StringBuilder stringBuilder = new(); |
|
|
|
|
stringBuilder.Append(parameter.Type.Name); |
|
|
|
|
|
|
|
|
|
if (parameter.Type is INamedTypeSymbol { IsGenericType: true } namedTypeSymbol) |
|
|
|
|
stringBuilder.Append(GetGenericArguments(namedTypeSymbol)); |
|
|
|
|
|
|
|
|
|
if (parameter.NullableAnnotation == NullableAnnotation.Annotated) |
|
|
|
|
stringBuilder.Append("?"); |
|
|
|
|
|
|
|
|
|
@ -312,13 +312,24 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
|
return stringBuilder.ToString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private List<string> GetParameterConstraints(ITypeParameterSymbol typeParameterSymbol) |
|
|
|
|
private string GetGenericArguments(INamedTypeSymbol namedTypeSymbol) => $"<{string.Join(", ", namedTypeSymbol.TypeArguments.Select(GetGenericArgument))}>"; |
|
|
|
|
private string GetGenericArgument(ITypeSymbol argument) |
|
|
|
|
{ |
|
|
|
|
List<string> constraints = []; |
|
|
|
|
StringBuilder stringBuilder = new(); |
|
|
|
|
stringBuilder.Append(argument.Name); |
|
|
|
|
|
|
|
|
|
foreach (ITypeSymbol constraintType in typeParameterSymbol.ConstraintTypes) |
|
|
|
|
constraints.Add(constraintType.Name); |
|
|
|
|
if (argument is INamedTypeSymbol { IsGenericType: true } namedTypeSymbol) |
|
|
|
|
stringBuilder.Append(GetGenericArguments(namedTypeSymbol)); |
|
|
|
|
|
|
|
|
|
if (argument.NullableAnnotation == NullableAnnotation.Annotated) |
|
|
|
|
stringBuilder.Append("?"); |
|
|
|
|
|
|
|
|
|
return stringBuilder.ToString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private List<string> GetParameterConstraints(ITypeParameterSymbol typeParameterSymbol) |
|
|
|
|
{ |
|
|
|
|
List<string> constraints = []; |
|
|
|
|
if (typeParameterSymbol.HasReferenceTypeConstraint) |
|
|
|
|
constraints.Add("class"); |
|
|
|
|
|
|
|
|
|
@ -334,6 +345,14 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
|
if (typeParameterSymbol.HasNotNullConstraint) |
|
|
|
|
constraints.Add("notnull"); |
|
|
|
|
|
|
|
|
|
foreach (ITypeSymbol constraintType in typeParameterSymbol.ConstraintTypes) |
|
|
|
|
{ |
|
|
|
|
if (constraintType is INamedTypeSymbol { IsGenericType: true } namedTypeSymbol) |
|
|
|
|
constraints.Add($"{constraintType.Name}{GetGenericArguments(namedTypeSymbol)}"); |
|
|
|
|
else |
|
|
|
|
constraints.Add(constraintType.Name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return constraints; |
|
|
|
|
} |
|
|
|
|
} |