Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
36 changes: 33 additions & 3 deletions EasySourceGenerators.Generators/GeneratesMethodExecutionRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,43 @@ internal static IReadOnlyList<IMethodSymbol> 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)
Expand Down
4 changes: 2 additions & 2 deletions EasySourceGenerators.Tests/SimpleMethodWithParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace EasySourceGenerators.Tests;

partial class SimpleMethodWithParameterClass
{
public partial string SimpleMethodWithParameter()
public partial int SimpleMethodWithParameter(int someIntParameter)
{
return 5;
}
Expand All @@ -43,4 +43,4 @@ private static int SimpleMethodWithParameter_Generator(int someIntParameter)
{
return 5;
}
}
}