小伙伴都知道在 Windows 下是支持文件名使用分號的,而寫過 Roslyn 的小伙伴都知道,在 csproj 項目裡面使用分號分割數組。那麼在 VS 裡面將一個文件名添加分號會如何?下麵讓咱寫寫看 ...
小伙伴都知道在 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 歡迎小伙伴訪問