How to use a resx resource file in a T4 template

后端 未结 4 1056
不思量自难忘°
不思量自难忘° 2021-01-03 11:24

I cant figure out how to include the resource file (.resx) in the (.tt) T4 template.

I tried so far... Importing the namespace

<#@ import names         


        
4条回答
  •  清酒与你
    2021-01-03 11:57

    If you want to access the resources of a .resx-File from within a T4 template, this is how you would do it:

    1. In the Resource Editor set the Access Modifier for the resources to "Public".
    2. Make sure your project builds successfully. The t4 template can only access the output assembly - so check if it's up to date.
    3. Reference the output assembly in the T4 template (you can use Visual Studio Macros here): <#@ assembly name="$(TargetDir)\outputfile.ext" #>
    4. Import the namespace of the ResourceFile in the T4 template <#@ import namespace="MyNamespace" #>

    Then you can access the resources as usually:

    <# var theResource = Resource1.TheResource; #>
    

提交回复
热议问题