1
當下面的代碼在MSBuild中運行時,我得到一個InvalidOperationException。我想知道爲什麼這是?爲什麼我不能在自定義任務構造函數中使用MsBuild TaskLoggingHelper?
public class SimpleTask3 : Task
{
public SimpleTask3()
{
Log.LogMessage(MessageImportance.High, "A MESSAGE");
}
public override bool Execute()
{
return true;
}
}
收到完整的錯誤如下
error MSB4061: The "SimpleTask3" task could not be instantiated from ...ConsoleApplication1.dll
error MSB4061: System.InvalidOperationException: Task attempted to log before it was initialized. Message was: A MESSAGE
error MSB4061: at Microsoft.Build.Shared.ErrorUtilities.ThrowInvalidOperation(String resourceName, Object[] args)
error MSB4061: at Microsoft.Build.Utilities.TaskLoggingHelper.LogMessage(MessageImportance importance, String message, Object[] messageArgs)
error MSB4061: at SimpleTask3.SimpleTask3..ctor() in SimpleTask.cs:line 10
error MSB4060: The "SimpleTask3" task hasbeen declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.
感謝您的解釋。你能解釋一下怎樣和爲什麼?我知道它不能幫助我,但我更感興趣。再次感謝。 –
我現在不是真的爲什麼這樣做,因爲它沒有在代碼中分解,但一個可能的原因是,構造函數保持簡單,因此您可以編寫無參數的構造函數而不必麻煩傳遞參數到基類構造函數,而其餘的代碼負責設置BuildEngine而不必關心它。在這裏完成:https://github.com/Microsoft/msbuild/blob/master/src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs#L369 – stijn