Skip to content

Commit dfa4229

Browse files
committed
Fixed "no method List.ToArray" with params registration.
1 parent 84942f3 commit dfa4229

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

Source/ExcelDna.SourceGenerator.NativeAOT/Generator.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void Execute(GeneratorExecutionContext context)
3232
// <auto-generated/>
3333
using System;
3434
using System.Collections.Generic;
35+
using System.Reflection;
3536
using System.Runtime.CompilerServices;
3637
using System.Runtime.InteropServices;
3738
@@ -46,7 +47,7 @@ public static short Initialize(void* xlAddInExportInfoAddress, void* hModuleXll,
4647
4748
[ADDINS]
4849
49-
[FUNCTIONS]
50+
[FUNCTIONS]
5051
5152
return ExcelDna.ManagedHost.AddInInitialize.InitializeNativeAOT(xlAddInExportInfoAddress, hModuleXll, pPathXLL, disableAssemblyContextUnload, pTempDirPath);
5253
}
@@ -70,16 +71,24 @@ public static short Initialize(void* xlAddInExportInfoAddress, void* hModuleXll,
7071
source = source.Replace("[ADDINS]", addIns);
7172
}
7273
{
73-
string functions = "List<Type> functionTypes = new List<Type>();\r\n";
74+
string functions = "List<Type> typeRefs = new List<Type>();\r\n";
75+
string methods = "List<MethodInfo> methodRefs = new List<MethodInfo>();\r\n";
7476
foreach (var i in receiver.Functions)
7577
{
7678
functions += $"ExcelDna.Integration.NativeAOT.MethodsForRegistration.Add(typeof({Util.GetFullTypeName(i.ContainingType)}).GetMethod(\"{i.Name}\")!);\r\n";
77-
functions += $"functionTypes.Add(typeof({Util.MethodType(i)}));\r\n";
79+
functions += $"typeRefs.Add(typeof({Util.MethodType(i)}));\r\n";
7880
foreach (var p in i.Parameters)
79-
functions += $"functionTypes.Add(typeof(Func<object, {Util.GetFullTypeName(p.Type)}>));\r\n";
81+
{
82+
functions += $"typeRefs.Add(typeof(Func<object, {Util.GetFullTypeName(p.Type)}>));\r\n";
83+
}
8084
functions += "\r\n";
85+
86+
if (i.Parameters.Length > 0 && i.Parameters.Last().IsParams && i.Parameters.Last().Type is IArrayTypeSymbol arrayType)
87+
{
88+
methods += $"methodRefs.Add(typeof(List<{Util.GetFullTypeName(arrayType.ElementType)}>).GetMethod(\"ToArray\")!);\r\n";
89+
}
8190
}
82-
source = source.Replace("[FUNCTIONS]", functions);
91+
source = source.Replace("[FUNCTIONS]", functions + methods);
8392
}
8493

8594
context.AddSource($"ExcelDna.SG.NAOT.Init.g.cs", source);

Source/Tests/ExcelDna.SourceGenerator.NativeAOT.Tests/Generator.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,48 @@ public class Generator
55
[Fact]
66
public void Empty()
77
{
8-
SourceGeneratorDriver.Verify("", """
8+
VerifyFunctions("", """
9+
List<Type> typeRefs = new List<Type>();
10+
List<MethodInfo> methodRefs = new List<MethodInfo>();
11+
""");
12+
}
13+
14+
[Fact]
15+
public void Params()
16+
{
17+
VerifyFunctions("""
18+
using ExcelDna.Integration;
19+
20+
namespace ExcelDna.AddIn.RuntimeTestsAOT
21+
{
22+
public class Functions
23+
{
24+
[ExcelFunction]
25+
public static string NativeParamsJoinString(string separator, params string[] values)
26+
{
27+
return string.Join(separator, values);
28+
}
29+
}
30+
}
31+
""", """
32+
List<Type> typeRefs = new List<Type>();
33+
ExcelDna.Integration.NativeAOT.MethodsForRegistration.Add(typeof(ExcelDna.AddIn.RuntimeTestsAOT.Functions).GetMethod("NativeParamsJoinString")!);
34+
typeRefs.Add(typeof(Func<string, string[], string>));
35+
typeRefs.Add(typeof(Func<object, string>));
36+
typeRefs.Add(typeof(Func<object, string[]>));
37+
38+
List<MethodInfo> methodRefs = new List<MethodInfo>();
39+
methodRefs.Add(typeof(List<string>).GetMethod("ToArray")!);
40+
""");
41+
}
42+
43+
private static void VerifyFunctions(string sourceCode, string functions)
44+
{
45+
string template = """
946
// <auto-generated/>
1047
using System;
1148
using System.Collections.Generic;
49+
using System.Reflection;
1250
using System.Runtime.CompilerServices;
1351
using System.Runtime.InteropServices;
1452
@@ -23,14 +61,15 @@ public static short Initialize(void* xlAddInExportInfoAddress, void* hModuleXll,
2361
2462
2563
26-
List<Type> functionTypes = new List<Type>();
64+
[FUNCTIONS]
2765
2866
2967
return ExcelDna.ManagedHost.AddInInitialize.InitializeNativeAOT(xlAddInExportInfoAddress, hModuleXll, pPathXLL, disableAssemblyContextUnload, pTempDirPath);
3068
}
3169
}
3270
}
33-
""");
71+
""";
72+
SourceGeneratorDriver.Verify(sourceCode, template.Replace("[FUNCTIONS]", functions));
3473
}
3574
}
3675
}

Source/Tests/ExcelDna.SourceGenerator.NativeAOT.Tests/SourceGeneratorDriver.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static void Verify(string sourceCode, string expected)
1212
[
1313
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
1414
MetadataReference.CreateFromFile(System.Reflection.Assembly.Load("System.Runtime").Location),
15+
MetadataReference.CreateFromFile(System.Reflection.Assembly.Load("System.Collections").Location),
1516
MetadataReference.CreateFromFile(typeof(ExcelDna.Integration.NativeAOT).Assembly.Location),
1617
MetadataReference.CreateFromFile(typeof(ExcelDna.ManagedHost.AddInInitialize).Assembly.Location),
1718
],
@@ -21,7 +22,8 @@ public static void Verify(string sourceCode, string expected)
2122

2223
Assert.Empty(diagnostics);
2324
Assert.True(outputCompilation.SyntaxTrees.Count() == 2);
24-
Assert.Empty(outputCompilation.GetDiagnostics());
25+
var outputCompilationDiagnostics = outputCompilation.GetDiagnostics();
26+
Assert.Empty(outputCompilationDiagnostics);
2527

2628
GeneratorDriverRunResult runResult = driver.GetRunResult();
2729
Assert.True(runResult.GeneratedTrees.Length == 1);

0 commit comments

Comments
 (0)