问题
I am trying to build the PostgreSqlGeneration
code from this repository on Mono. Unfortunately I get an error I do not understand.
In the PostgreSqlMigrationSqlGenerator class the following method gives the build error "delegate System.Action does not take `1' arguments":
private void GenerateStatements(IEnumerable<MigrationOperation> migrationOperations)
{
Check.NotNull(migrationOperations, "migrationOperations");
DetectHistoryRebuild(migrationOperations).Each<dynamic>(o => Generate(o)); // <=here!
}
/edit The signature of the extension method is as follows:
/edit 2. Here is the declaration for Generate
method:
private void Generate(HistoryOperation migration)
{
//migration
Check.NotNull(migration, "historyOperation");
using (var writer = Writer())
{
migration.CommandTrees.Each(
commandTree =>
{
switch (commandTree.CommandTreeKind)
{
case DbCommandTreeKind.Insert:
writer.Write(GetInsertHistorySql((DbInsertCommandTree)commandTree));
break;
}
});
Statement(writer);
}
}
I do not know why that happens since the Each
only has a dynamic
type and no integer one. But I am not that experienced with such lambda expressions. To learn more and to get the migrations to work I hope someone can explain why the error happens and how it can be fixed.
回答1:
Disclaimer: I feel really bad that I cannot find anything that explains why this isn't working. If someone knows; please tell me. Google has failed here.
Clearly the compiler is picking the wrong overload for Each
. There are two in the library, one that takes an Action<T>
and another that takes an Action<T, int>
.
If you weren't using dynamic
it would work fine (if I had to guess); but dynamic
causes all sorts of weird issues; plus you are using Mono.
Since the compiler insists you use the other overload, the solution is simple enough. Just use it!
DetectHistoryRebuild(migrationOperations).Each<dynamic>((o, i) => Generate(o));
You took an extra parameter and didn't use it. Its not the end of the world.
You could also just explicitly instantiate the Action
so the compiler doesn't have to choose:
DetectHistoryRebuild(migrationOperations).Each<dynamic>(new Action(o => Generate(o)));
回答2:
The solution was to add a missing assembly reference of Microsoft.Csharp.dll
. For some reason the error of a missing assembly reference became visible after changing the lambda signature from i
to (i,j)
as suggested by BradleyDotNET in his answer.
来源:https://stackoverflow.com/questions/27068298/delegate-system-actiondynamic-int-does-not-take-1-arguments