繁体   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