Injecting DI service on a extension method

后端 未结 2 1342
旧时难觅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:21

    Why not just pass the IStringLocalizer as a parameter:

    public static string GetName(this Type type, IStringLocalizer localizer)
    {
        return localizer[type.Name].Value;
    }
    

    The purpose of extension methods is to extend behavior of objects. It seems to me that is what you're trying to do here.

提交回复
热议问题