Skip to content

Commit 8f9d89d

Browse files
Copilotdex3r
andcommitted
Fix simple generator invocation for parameterized methods
Co-authored-by: dex3r <[email protected]>
1 parent 5a5d93e commit 8f9d89d

3 files changed

Lines changed: 62 additions & 5 deletions

File tree

EasySourceGenerators.GeneratorTests/GeneratesMethodExecutionRuntimeTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,33 @@ public static class GenHost
9090
Assert.That(result.value, Is.EqualTo("hello"));
9191
}
9292

93+
[Test]
94+
public void ExecuteSimpleGeneratorMethod_ExecutesStaticMethodWithParametersUsingDefaultValues()
95+
{
96+
CSharpCompilation compilation = CreateCompilation("""
97+
namespace TestNamespace;
98+
99+
public partial class Target
100+
{
101+
public partial int GetValue(int input);
102+
}
103+
104+
public static class GenHost
105+
{
106+
public static int Generate(int input) => input + 5;
107+
}
108+
""");
109+
110+
IMethodSymbol generatorMethod = GetMethodSymbol(compilation, "TestNamespace.GenHost", "Generate");
111+
IMethodSymbol partialMethod = GetMethodSymbol(compilation, "TestNamespace.Target", "GetValue");
112+
113+
(string? value, string? error) result =
114+
GeneratesMethodExecutionRuntime.ExecuteSimpleGeneratorMethod(generatorMethod, partialMethod, compilation);
115+
116+
Assert.That(result.error, Is.Null);
117+
Assert.That(result.value, Is.EqualTo("5"));
118+
}
119+
93120
[Test]
94121
public void ExecuteGeneratorMethodWithArgs_ConvertsArgumentsToMethodParameterType()
95122
{

EasySourceGenerators.Generators/GeneratesMethodExecutionRuntime.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,43 @@ internal static IReadOnlyList<IMethodSymbol> GetAllUnimplementedPartialMethods(C
273273

274274
private static object?[]? ConvertArguments(object?[]? args, MethodInfo methodInfo)
275275
{
276-
if (args == null || methodInfo.GetParameters().Length == 0)
276+
ParameterInfo[] parameters = methodInfo.GetParameters();
277+
if (parameters.Length == 0)
277278
{
278279
return null;
279280
}
280281

281-
Type parameterType = methodInfo.GetParameters()[0].ParameterType;
282-
return new[] { Convert.ChangeType(args[0], parameterType) };
282+
if (args != null && args.Length > parameters.Length)
283+
{
284+
throw new TargetParameterCountException();
285+
}
286+
287+
object?[] convertedArguments = new object?[parameters.Length];
288+
for (int i = 0; i < parameters.Length; i++)
289+
{
290+
ParameterInfo parameter = parameters[i];
291+
object? argument = args != null && i < args.Length ? args[i] : null;
292+
if (argument == null)
293+
{
294+
convertedArguments[i] = GetDefaultValue(parameter.ParameterType);
295+
continue;
296+
}
297+
298+
Type targetType = Nullable.GetUnderlyingType(parameter.ParameterType) ?? parameter.ParameterType;
299+
convertedArguments[i] = Convert.ChangeType(argument, targetType);
300+
}
301+
302+
return convertedArguments;
303+
}
304+
305+
private static object? GetDefaultValue(Type type)
306+
{
307+
if (!type.IsValueType || Nullable.GetUnderlyingType(type) != null)
308+
{
309+
return null;
310+
}
311+
312+
return Activator.CreateInstance(type);
283313
}
284314

285315
private static SwitchBodyData ExtractSwitchBodyData(object lastRecord, ITypeSymbol returnType)

EasySourceGenerators.Tests/SimpleMethodWithParameter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace EasySourceGenerators.Tests;
2323
2424
partial class SimpleMethodWithParameterClass
2525
{
26-
public partial string SimpleMethodWithParameter()
26+
public partial int SimpleMethodWithParameter(int someIntParameter)
2727
{
2828
return 5;
2929
}
@@ -43,4 +43,4 @@ private static int SimpleMethodWithParameter_Generator(int someIntParameter)
4343
{
4444
return 5;
4545
}
46-
}
46+
}

0 commit comments

Comments
 (0)