diff --git a/EasySourceGenerators.GeneratorTests/GeneratesMethodExecutionRuntimeTests.cs b/EasySourceGenerators.GeneratorTests/GeneratesMethodExecutionRuntimeTests.cs index 2da4545..701d7a1 100644 --- a/EasySourceGenerators.GeneratorTests/GeneratesMethodExecutionRuntimeTests.cs +++ b/EasySourceGenerators.GeneratorTests/GeneratesMethodExecutionRuntimeTests.cs @@ -90,6 +90,33 @@ public static class GenHost Assert.That(result.value, Is.EqualTo("hello")); } + [Test] + public void ExecuteSimpleGeneratorMethod_ExecutesStaticMethodWithParametersUsingDefaultValues() + { + CSharpCompilation compilation = CreateCompilation(""" + namespace TestNamespace; + + public partial class Target + { + public partial int GetValue(int input); + } + + public static class GenHost + { + public static int Generate(int input) => input + 5; + } + """); + + IMethodSymbol generatorMethod = GetMethodSymbol(compilation, "TestNamespace.GenHost", "Generate"); + IMethodSymbol partialMethod = GetMethodSymbol(compilation, "TestNamespace.Target", "GetValue"); + + (string? value, string? error) result = + GeneratesMethodExecutionRuntime.ExecuteSimpleGeneratorMethod(generatorMethod, partialMethod, compilation); + + Assert.That(result.error, Is.Null); + Assert.That(result.value, Is.EqualTo("5")); + } + [Test] public void ExecuteGeneratorMethodWithArgs_ConvertsArgumentsToMethodParameterType() { diff --git a/EasySourceGenerators.Generators/GeneratesMethodExecutionRuntime.cs b/EasySourceGenerators.Generators/GeneratesMethodExecutionRuntime.cs index 8efb3fa..3d74c16 100644 --- a/EasySourceGenerators.Generators/GeneratesMethodExecutionRuntime.cs +++ b/EasySourceGenerators.Generators/GeneratesMethodExecutionRuntime.cs @@ -273,13 +273,43 @@ internal static IReadOnlyList GetAllUnimplementedPartialMethods(C private static object?[]? ConvertArguments(object?[]? args, MethodInfo methodInfo) { - if (args == null || methodInfo.GetParameters().Length == 0) + ParameterInfo[] parameters = methodInfo.GetParameters(); + if (parameters.Length == 0) { return null; } - Type parameterType = methodInfo.GetParameters()[0].ParameterType; - return new[] { Convert.ChangeType(args[0], parameterType) }; + if (args != null && args.Length > parameters.Length) + { + throw new TargetParameterCountException(); + } + + object?[] convertedArguments = new object?[parameters.Length]; + for (int parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++) + { + ParameterInfo parameter = parameters[parameterIndex]; + object? argument = args != null && parameterIndex < args.Length ? args[parameterIndex] : null; + if (argument == null) + { + convertedArguments[parameterIndex] = GetDefaultValue(parameter.ParameterType); + continue; + } + + Type targetType = Nullable.GetUnderlyingType(parameter.ParameterType) ?? parameter.ParameterType; + convertedArguments[parameterIndex] = Convert.ChangeType(argument, targetType); + } + + return convertedArguments; + } + + private static object? GetDefaultValue(Type type) + { + if (!type.IsValueType || Nullable.GetUnderlyingType(type) != null) + { + return null; + } + + return Activator.CreateInstance(type); } private static SwitchBodyData ExtractSwitchBodyData(object lastRecord, ITypeSymbol returnType) diff --git a/EasySourceGenerators.Tests/SimpleMethodWithParameter.cs b/EasySourceGenerators.Tests/SimpleMethodWithParameter.cs index 526bfc6..2bca91e 100644 --- a/EasySourceGenerators.Tests/SimpleMethodWithParameter.cs +++ b/EasySourceGenerators.Tests/SimpleMethodWithParameter.cs @@ -23,7 +23,7 @@ namespace EasySourceGenerators.Tests; partial class SimpleMethodWithParameterClass { - public partial string SimpleMethodWithParameter() + public partial int SimpleMethodWithParameter(int someIntParameter) { return 5; } @@ -43,4 +43,4 @@ private static int SimpleMethodWithParameter_Generator(int someIntParameter) { return 5; } -} \ No newline at end of file +}