Best practices: C# Extension methods namespace and promoting extension methods

后端 未结 10 1012
南方客
南方客 2021-02-02 06:43

I know there exists already a post, describing nearly the same, but I think mine is a bit different.

What I would like to know is how you organize your extension methods

10条回答
  •  后悔当初
    2021-02-02 07:24

    I like the way ReSharper solves this problem.

    ReSharper discovers any available extension methods, even without the corresponding usings. In case the using is not present, Intellisense also shows the namespace where the extension resides, making clear where the extension comes from and indicating that selecting it will add the using. (Example below.)

    Naturally, only namespaces reachable by the current project, i.e. directly or indirectly referenced, are included.

    Here is an example of what Intellisense might show if there are two extension methods. The first one comes from a namespace that we have already included. The second comes from a namespace that we have not (yet) included.

    • AddMvc
    • AddEntityFrameworkSqlServer (Microsoft.Extensions.DependencyInjection)

提交回复
热议问题