简体   繁体   English

如何在 xslt 文件中使用 C# 表达式体法?

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

Here is C# method in the xslt file that compiles without errors:这是 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>

however if I change it to expression-bodied method但是,如果我将其更改为表达式主体方法

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

the following message shows and ten errors in the output: output 中显示以下消息和十个错误:

错误

Is it possible to use this syntax?是否可以使用这种语法?

The latest MSXML was released way before C# 6, so all these new language features are not supported.最新的 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. 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.

However, somehow the installation and search paths don't match so, as https://stackoverflow.com/a/40311406/252228 explains, the compiler is looked for in bin\roslyn while it is installed in roslyn .但是,不知何故,安装和搜索路径不匹配,因此,正如https://stackoverflow.com/a/40311406/252228解释的那样,编译器在安装在roslyn时在bin\roslyn中查找。

If I use the first option "move the \roslyn subdirectory to \bin\roslyn " (I did that by hand for the test), then C# code like your use of => inside msxsl:script compiles and runs with the .NET framework and XslCompiledTransform .如果我使用第一个选项“将\roslyn子目录移动到\bin\roslyn ”(我为测试手动完成了该操作),那么 C# 代码就像您在msxsl:script中使用=>一样编译并运行 .NET 框架和XslCompiledTransform

I tried that with the latest version 3.6.0 of the package named above and a C# .NET framework 4.7.2 console application run in VS 2019.我尝试使用上面命名的 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 and the above setup it didn't fix the problem, so the only successful test I have been able to perform is to move the directory with Windows Explorer. 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