Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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" #>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top