ReSharper warns: “Static field in generic type”

后端 未结 4 1574
一生所求
一生所求 2020-12-12 21:41
public class EnumRouteConstraint : IRouteConstraint
    where T : struct
{
    private static readonly Lazy> _enumNames; // <         


        
4条回答
  •  长情又很酷
    2020-12-12 22:11

    From the JetBrains wiki:

    In the vast majority of cases, having a static field in a generic type is a sign of an error. The reason for this is that a static field in a generic type will not be shared among instances of different close constructed types. This means that for a generic class C which has a static field X, the values of C.X and C.X have completely different, independent values.

    In the rare cases when you do need the 'specialized' static fields, feel free to suppress the warning.

    If you need to have a static field shared between instances with different generic arguments, define a non-generic base class to store your static members, then set your generic type to inherit from this type.

提交回复
热议问题