簡體   English   中英

如何在 xslt 文件中使用 C# 表達式體法?

[英]How to use C# expression-bodied method in the xslt file?

這是 xslt 文件中的 C# 方法,編譯時沒有錯誤:

  <msxsl:script language="C#" implements-prefix="user">
    <msxsl:assembly name="System.Core" />
    <msxsl:using namespace="System.IO" />
    <msxsl:using namespace="System.Collections.Generic"/>
    <msxsl:using namespace="System.Text.RegularExpressions" />
    <msxsl:using namespace="System.Linq" />
    <![CDATA[
    public string GetDesc(string state, string licNum, string date)
    {
       return string.Join("; ", new[]{ "State: " + state, "DL Number: " + licNum, "Date: " + 
       date }.Where(s => !string.IsNullOrWhiteSpace(s)));
    }
        ]]>
    </msxsl:script>

但是,如果我將其更改為表達式主體方法

public string GetDesc(string state, string licNum, string date) => 
string.Join("; ", new[]{ "State: " + state, "DL Number: " + licNum, "Date: " + date }
.Where(s => !string.IsNullOrWhiteSpace(s)));

output 中顯示以下消息和十個錯誤:

錯誤

是否可以使用這種語法?

最新的 MSXML 在 C# 6 之前發布,因此不支持所有這些新語言功能。

It should be possible to install the NuGet package https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/ , that way XslCompiledTransform tries to use the Roslyn C# CodeDomProvider with the newer C# features.

但是,不知何故,安裝和搜索路徑不匹配,因此,正如https://stackoverflow.com/a/40311406/252228解釋的那樣,編譯器在安裝在roslyn時在bin\roslyn中查找。

如果我使用第一個選項“將\roslyn子目錄移動到\bin\roslyn ”(我為測試手動完成了該操作),那么 C# 代碼就像您在msxsl:script中使用=>一樣編譯並運行 .NET 框架和XslCompiledTransform

我嘗試使用上面命名的 package 的最新版本 3.6.0 以及在 VS 201.9 中運行的 C# .NET 框架 4.7.2 控制台應用程序

There is also the package https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.BinFix/ that is supposed to be installed in addition to the DotNetCompilerPlatform one to fix the installation path but somehow for me with VS 2019 和上述設置並沒有解決問題,所以我能夠執行的唯一成功測試是使用 Windows Explorer 移動目錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM