locked
Using extension methods from a separate class library in T4 RRS feed

  • Question

  • I created a class library with extension methods for working with EnvDTE objects. An example extension method is:

    namespace EnvDTE.Extensions{    public static class DTEExtensions    {        public static Project CurrentProject (this DTE dte)        {            return dte.ActiveDocument.ProjectItem.ContainingProject;        }    }}


    when I attempt to use the extension method in my T4 file:

    <#@ template debug="false" hostspecific="true" language="C#" #><#@ assembly name="EnvDTE" #><#@ assembly name = "$(SolutionDir)..\..\Windows\EnvDTE.Extensions\EnvDTE.Extensions\bin\Debug\EnvDTE.Extensions.dll" #><#@ assembly name = "System.Core" #><#@ import namespace="EnvDTE" #><#@ import namespace="EnvDTE.Extensions" #><#@ import namespace="System.IO" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #><#@ output extension=".cs" #><#    var serviceProvider = (IServiceProvider)this.Host;    var dte = serviceProvider.GetService(typeof(DTE)) as DTE;     var project = dte.CurrentProject();#>namespace <#= project.Properties.Item("DefaultNamespace").Value #>{}


    I get the following exception:

    Severity    Code    Description Project File    LineError       Running transformation: System.Runtime.Remoting.RemotingException: Cannot load type 'EnvDTE._DTE, EnvDTE.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.Server stack trace:    at System.Runtime.Remoting.Messaging.MethodCall.ResolveMethod(Boolean bThrowIfNotResolved)   at System.Runtime.Remoting.Messaging.MethodCall..ctor(SmuggledMethodCallMessage smuggledMsg, ArrayList deserializedArgs)   at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoDispatch(Byte[] reqStmBuff, SmuggledMethodCallMessage smuggledMcm, SmuggledMethodReturnMessage& smuggledMrm)   at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoTransitionDispatchCallback(Object[] args)Exception rethrown at [0]:    at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)   at EnvDTE._DTE.get_ActiveDocument()   at EnvDTE.Extensions.DTEExtensions.CurrentProject(DTE dte) in D:\Users\Adaptive\Projects\Windows\EnvDTE.Extensions\EnvDTE.Extensions\DTEExtensions.cs:line 13   at Microsoft.VisualStudio.TextTemplating0BE14095FFF8852C1DA8AAA0F831675849ECBB71E2A8B8BA2027BD64107CEDE73824E8CF863A0048B6B5FF80E64944B13E838B3338BD67CA7B6A2CF9BB4BD07C.GeneratedTextTransformation.TransformText() TicketTracker   D:\Users\Adaptive\Projects\Universal\TicketTracker\TicketTracker\Models\HoursModel.tt   1


    Any idea what I'm doing wrong here? It appears the transformation engine is looking for types in the wrong library. I know I could create a collection of my extension methods by importing various T4 files, but I prefer a class library if I can make it work.

    Thursday, November 26, 2015 10:21 PM

Answers

  • Hi Alfërò,

    A workaround from web for your reference.

    Select the EnvDTE assembly from your project. Right click and choose property window. Change the Embed Interop Types value to False.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Adaptve Tuesday, December 1, 2015 3:58 PM
    Tuesday, December 1, 2015 9:05 AM

All replies

  • Hi Alfërò,

    Since this issue is related to the Extension, I help you move this case to the Visual Studio Integrate forum for dedicated support.

    Thanks,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 30, 2015 2:43 AM
  • Hi Alfërò,

    When you want to use DTE in your T4 template, you have to import these assemblies to your T4 template.

    <#@ assembly name="Microsoft.VisualStudio.Shell.Interop.8.0" #>
    <#@ assembly name="EnvDTE" #>
    <#@ assembly name="EnvDTE80" #>
    <#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
    <#@ import namespace="EnvDTE" #>
    <#@ import namespace="EnvDTE80" #>
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Edited by DotNet Wang Monday, November 30, 2015 6:33 AM
    Monday, November 30, 2015 6:32 AM
  • Hi Li,

    Thank you so much for your reply. I'm sad to report that your suggestion did not work.

    Please note that the T4 runtime is able to locate the right extension method, and execute it. However, inside the method:

    public static Project CurrentProject (this DTE dte)        {            
    return dte.ActiveDocument.ProjectItem.ContainingProject;        
    }

    The runtime gets confused as it tries to locate: 'EnvDTE._DTE' interface inside my custom 'EnvDTE.Extensions' assembly. The interface is actually located inside the 'EnvDTE' assembly.

    System.Runtime.Remoting.RemotingException

    Could it be because of the name I gave to my assembly?


    • Edited by Adaptve Monday, November 30, 2015 9:17 PM
    Monday, November 30, 2015 8:14 PM
  • Hi Alfërò,

    A workaround from web for your reference.

    Select the EnvDTE assembly from your project. Right click and choose property window. Change the Embed Interop Types value to False.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Adaptve Tuesday, December 1, 2015 3:58 PM
    Tuesday, December 1, 2015 9:05 AM
  • Yay, that worked!

    Thank you very much!

    Tuesday, December 1, 2015 3:58 PM