diff --git a/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs b/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs
index d077c4f8ec7..3015b147f20 100644
--- a/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs
+++ b/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs
@@ -1054,6 +1054,26 @@ public void InferredTypeFallsBackToStringWhenTypeConversionFails()
@"[assembly: System.Diagnostics.DebuggableAttribute(true, ""42"")]");
}
+ ///
+ /// If the parameter type cannot be found,
+ /// then the name of positional parameter should be displayed in the log.
+ ///
+ [Fact]
+ public void MessageDisplayPositionalParameterNameWhenAttributeNotFound()
+ {
+ WriteCodeFragment task = new WriteCodeFragment();
+ MockEngine engine = new MockEngine(true);
+ task.BuildEngine = engine;
+ TaskItem attribute = new TaskItem("System.TheAttributeCannotFound");
+ attribute.SetMetadata("_Parameter1", "true");
+ task.AssemblyAttributes = new TaskItem[] { attribute };
+ task.Language = "C#";
+ task.OutputDirectory = new TaskItem(Path.GetTempPath());
+ bool result = task.Execute();
+
+ engine.AssertLogContains("Could not infer the type of parameter \"_Parameter1\" because the attribute type is unknown. The value will be treated as a string.");
+ }
+
///
/// Individual parameters can be typed differently.
///
diff --git a/src/Tasks/WriteCodeFragment.cs b/src/Tasks/WriteCodeFragment.cs
index 9e08cea56bc..f6e07a804e5 100644
--- a/src/Tasks/WriteCodeFragment.cs
+++ b/src/Tasks/WriteCodeFragment.cs
@@ -229,7 +229,7 @@ private string GenerateCode(out string extension)
}
// "_Parameter01" and "_Parameter1" would overwrite each other
- orderedParameters[index - 1] = new AttributeParameter { Type = type, Value = value };
+ orderedParameters[index - 1] = new AttributeParameter { Type = type, Value = value, PositionalParameterName = name };
}
else
{
@@ -449,7 +449,7 @@ private bool AddArguments(
value = ConvertParameterValueToInferredType(
constructorParameterTypes[i],
parameter.Value,
- $"#{i + 1}"); /* back to 1 based */
+ parameter.PositionalParameterName);
}
else
{
@@ -624,6 +624,7 @@ private struct AttributeParameter
{
public ParameterType Type { get; init; }
public string Name { get; init; }
+ public string PositionalParameterName { get; init; }
public string Value { get; init; }
}
}