Injecting DI service on a extension method

后端 未结 2 1343
旧时难觅i
旧时难觅i 2021-01-12 15:07

I\'m trying to get the IStringLocalizer service instance inside a extension method, is it possible? Any suggestions on how should I inject it?

My goal h

2条回答
  •  死守一世寂寞
    2021-01-12 15:18

    Following @NightOwl888 comment I was in the wrong path, I ended up creating the following service:

    public class TypeNameLocalizer : ITypeNameLocalizer
    {
        private IStringLocalizer localizer;
    
        public TypeNameLocalizer(IStringLocalizer localizer) 
        {
            this.localizer = localizer;
        }
        public string this[Type type] 
        { 
            get
            {
                return localizer[type.Name];
            }
        }
    }
    

    Credit: @NightOwl888

提交回复
热议问题