T4中的扩展方法

jlv*_*ero 5 vb.net t4 .net-3.5 visual-studio-2008 tangible-t4-editor

我试图在VS2008的T4模板中使用PGK.Extensions for VB.NET,我得到:

RemoveAllSpecialCharacters不是字符串的成员..

我的T4标题:

<#@ template language="VB" hostspecific="false" debug="true" inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" #>
<#@ output extension="vb" #>

<#@ assembly name="PGK.Extensions.dll" #> // the dll is found
<#@ import namespace="StringExtensions" #> //Try with and without namespace
Run Code Online (Sandbox Code Playgroud)

在块代码中使用扩展名:

<#
   Me.WriteLine(item.Name.RemoveAllSpecialCharacters.ToUpper)
#>
Run Code Online (Sandbox Code Playgroud)

RemoveAllSpecialCharacters是PGK.Extensions.dll的字符串扩展方法.

有谁能够帮我?

编辑:

好.它的:

<#@ template language="VBv3.5" ...
Run Code Online (Sandbox Code Playgroud)

但这打破了VS2008插件中的DevArt T4 Editor intellisense和语法高亮.请改用有形T4编辑器.

T. *_*bre 1

虽然扩展方法出现在要应用于对象的代码中,但实际上它被编译为静态方法。Microsoft 的此链接将为您提供更多相关信息。

因此,调用 item.Name.RemoveAllSpecialCharacters() 实际上被编译为StringExtensions .RemoveAllSpecialCharacters(item.Name)

因此,您可以尝试编写(未经测试,但应该有效):

<#
    StringExtensions.RemoveAllSpecialCharacters(item.Name).ToUpper()
#>
Run Code Online (Sandbox Code Playgroud)

希望有帮助。