小伙伴都知道在 Windows 下是支持檔案名使用分號的,而寫過 Roslyn 的小伙伴都知道,在 csproj 專案里面使用分號分割陣列,那么在 VS 里面將一個檔案名添加分號會如何?下面讓咱寫寫看
新建一個 WPF 專案,然后編輯檔案名,添加分號
在資源管理器的檔案如下
│ App.config
│ App.xaml
│ App.xaml.cs
│ JekacareqFebarbareei.csproj
│ MainWin;dow.xaml
│ MainWin;dow.xaml.cs
│
└─Properties
AssemblyInfo.cs
Resources.Designer.cs
Resources.resx
Settings.Designer.cs
Settings.settings
可以看到 MainWindow.xaml 修改為 MainWin;dow.xaml 此時雖然在 VS 可以打開,但是使用 dotnet build 構建,會提示下面代碼
MSBUILD : error MSB1025: An internal failure occurred while running MSBuild.
System.Reflection.TargetException: Object does not match target type.
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at Microsoft.Build.BackEnd.TaskHostTask.GetPropertyValue(TaskPropertyInfo property)
at Microsoft.Build.Execution.TaskFactoryWrapper.GetPropertyValue(ITask task, TaskPropertyInfo property)
at Microsoft.Build.BackEnd.TaskExecutionHost.GetItemOutputs(TaskPropertyInfo parameter)
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.GatherTaskOutputs(String parameterName, ElementLocation parameterLocation, Boolean outputTargetIsItem, String outputTargetName)
at Microsoft.Build.BackEnd.TaskBuilder.GatherTaskOutputs(ITaskExecutionHost taskExecutionHost, TaskExecutionMode howToExecuteTask, ItemBucket bucket)
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)
at Microsoft.Build.BackEnd.TaskBuilder.InitializeAndExecuteTask(TaskLoggingContext taskLoggingContext, ItemBucket bucket, IDictionary`2 taskIdentityParameters, TaskHost taskHost, TaskExecutionMode howToExecuteTask)
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteBucket(TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Dictionary`2 lookupHash)
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteTask(TaskExecutionMode mode, Lookup lookup)
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteTask(TargetLoggingContext loggingContext, BuildRequestEntry requestEntry, ITargetBuilderCallback targetBuilderCallback, ProjectTargetInstanceChild taskInstance, TaskExecutionMode mode, Lookup inferLookup, Lookup executeLookup, CancellationToken cancellationToken)
at Microsoft.Build.BackEnd.TargetEntry.ProcessBucket(ITaskBuilder taskBuilder, TargetLoggingContext targetLoggingContext, TaskExecutionMode mode, Lookup lookupForInference, Lookup lookupForExecution)
at Microsoft.Build.BackEnd.TargetEntry.ExecuteTarget(ITaskBuilder taskBuilder, BuildRequestEntry requestEntry, ProjectLoggingContext projectLoggingContext, CancellationToken cancellationToken)
at Microsoft.Build.BackEnd.TargetBuilder.ProcessTargetStack(ITaskBuilder taskBuilder)
at Microsoft.Build.BackEnd.TargetBuilder.BuildTargets(ProjectLoggingContext loggingContext, BuildRequestEntry entry, IRequestBuilderCallback callback, String[] targetNames, Lookup baseLookup, CancellationToken cancellationToken)
at Microsoft.Build.BackEnd.RequestBuilder.BuildProject()
at Microsoft.Build.BackEnd.RequestBuilder.BuildAndReport()
使用 msbuild 命令構建,會提示下面代碼
CoreCompile:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Roslyn\csc.exe /noconfig /nowarn:1
701,1702 /nostdlib+ /platform:anycpu32bitpreferred /errorreport:prompt /warn:4 /define:DEBUG;TRACE /highentropyva+ /r
eference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Microsoft.CSharp.dll" /r
eference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll" /reference
:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\PresentationCore.dll" /reference
:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\PresentationFramework.dll" /refe
rence:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll" /reference
:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.DataSetExtensions.dl
l" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.dll" /r
eference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.dll" /reference:"
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Net.Http.dll" /reference:"C
:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xaml.dll" /reference:"C:\Pro
gram Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xml.dll" /reference:"C:\Program F
iles (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xml.Linq.dll" /reference:"C:\Program Fi
les (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\WindowsBase.dll" /debug+ /debug:full /filealign
:512 /optimize- /out:obj\Debug\JekacareqFebarbareei.exe /ruleset:"C:\Program Files (x86)\Microsoft Visual Studio\2019
\Enterprise\Team Tools\Static Analysis Tools\\Rule Sets\MinimumRecommendedRules.ruleset" /subsystemversion:6.00 /targ
et:winexe /utf8output /deterministic+ /langversion:7.3 App.xaml.cs "MainWin;dow.xaml.cs" Properties\AssemblyInfo.cs P
roperties\Resources.Designer.cs Properties\Settings.Designer.cs "D:\lindexi\JekacareqFebarbareei\Je
kacareqFebarbareei\obj\Debug\MainWin" dow.g.cs "D:\lindexi\JekacareqFebarbareei\JekacareqFebarbaree
i\obj\Debug\App.g.cs"
Using shared compilation with compiler from directory: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise
\MSBuild\Current\Bin\Roslyn
CSC : error CS2001: Source file 'D:\lindexi\JekacareqFebarbareei\JekacareqFebarbareei\dow.g.cs' could
not be found. [D:\lindexi\JekacareqFebarbareei\JekacareqFebarbareei\JekacareqFebarbareei_cakuut2d_wp
ftmp.csproj]
CSC : error CS2001: Source file 'D:\lindexi\JekacareqFebarbareei\JekacareqFebarbareei\obj\Debug\MainW
in' could not be found.
可以看到在 CoreCompile 這里的輸出包含了 "D:\lindexi\JekacareqFebarbareei\Je kacareqFebarbareei\obj\Debug\MainWin" dow.g.cs "D:\lindexi\JekacareqFebarbareei\JekacareqFebarbaree i\obj\Debug\App.g.cs" 這就是將 MainWin;dow.xaml.cs 的輸出檔案 MainWin;dow.g.cs 分為兩個檔案,此時就發現找不到 dow.g.cs 檔案
在構建之后的 obj 檔案夾里面創建了 MainWin;dow.g.cs 檔案,所以檔案是對的,存在坑的是 msbuild 配置的設計問題,將分號作為陣列的元素分割,而檔案名可以使用分號,也就是如果檔案名存在分號,就將檔案分割為多個檔案
看到這里小伙伴就知道了,檔案名加上分號,會在構建的時候,因為設計問題,將一個檔案當成兩個檔案,于是就找不到檔案,構建不通過
調皮的小伙伴可不要在代碼檔案里面添加分號
本文代碼放在 github 歡迎小伙伴訪問
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/110.html
