문제

I'm trying to use PGK.Extensions in a T4 template in VS2008 for VB.NET and I get:

RemoveAllSpecialCharacters is not a member of string..

My T4 headers:

<#@ 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

Use of extension in block code:

<#
   Me.WriteLine(item.Name.RemoveAllSpecialCharacters.ToUpper)
#>

RemoveAllSpecialCharacters is a string extension method from PGK.Extensions.dll.

Can anybody help me?

EDITED:

OK. It's:

<#@ template language="VBv3.5" ...

But this breaks DevArt T4 Editor intellisense and syntax highlight in VS2008 plugin. Use Tangible T4 Editor instead.

도움이 되었습니까?

해결책

While an extension method appears from code to be applied on an object, it is in fact compiled as a static method. This link from Microsoft will give you more info on that.

So calling item.Name.RemoveAllSpecialCharacters() is in fact compiled as StringExtensions.RemoveAllSpecialCharacters(item.Name)

Therefore, you could try writing (not tested, but should work) :

<#
    StringExtensions.RemoveAllSpecialCharacters(item.Name).ToUpper()
#>

Hope that helps.

다른 팁

Had a hard time getting this to work myself, even with the blog post found here (also referenced by @jlvaquero in a comment above).

what finally worked for me was using the visual studio macro vars then import the namespace

 <#@ assembly name="$(ProjectDir)$(OutDir)\AssemblyName.dll" #>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top