相关疑难解决方法(0)

ReSharper警告:"通用类型的静态字段"

public class EnumRouteConstraint<T> : IRouteConstraint
    where T : struct
{
    private static readonly Lazy<HashSet<string>> _enumNames; // <--

    static EnumRouteConstraint()
    {
        if (!typeof(T).IsEnum)
        {
            throw new ArgumentException(
                Resources.Error.EnumRouteConstraint.FormatWith(typeof(T).FullName));
        }

        string[] names = Enum.GetNames(typeof(T));
        _enumNames = new Lazy<HashSet<string>>(() => new HashSet<string>
        (
            names.Select(name => name), StringComparer.InvariantCultureIgnoreCase
        ));
    }

    public bool Match(HttpContextBase httpContext, Route route, 
                        string parameterName, RouteValueDictionary values, 
                        RouteDirection routeDirection)
    {
        bool match = _enumNames.Value.Contains(values[parameterName].ToString());
        return match;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是错的吗?我会假设这实际上有一个static readonly字段用于EnumRouteConstraint<T>我碰巧实例化的每一个.

c# generics resharper static asp.net-mvc-3

251
推荐指数
4
解决办法
4万
查看次数

标签 统计

asp.net-mvc-3 ×1

c# ×1

generics ×1

resharper ×1

static ×1