How to use a resx resource file in a T4 template

后端 未结 4 1087
不思量自难忘°
不思量自难忘° 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 12:04

    There is an easier way to do this without compiling your project if you are using VS2010 SP1 and above, by using

    <#@ assembly name="$(TargetPath)" #>
    <#@ import namespace="Your.Namespace.Properties" #>
    

    Copy the first line as it is, and in the second line use the namespace where the Resource file resides and access the resource strings normally as you would in c#

    Resources.ResourceManager.GetString("SomeKey");
    

    or

    var key = Resources.SomeKey;
    

    I learnt this from Use class inside a T4 template

    Hope it helps somebody

提交回复
热议问题