SORU
10 Mart 2012, CUMARTESİ


ReSharper uyarıyor: "genel tür Statik alan"

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;
    }
}

Bu yanlış mı? Bu aslında örnek oldum olası her EnumRouteConstraint<T> static readonly bir alan olduğunu sanıyorum.

CEVAP
10 Mart 2012, CUMARTESİ


İyi genel bir tür statik bir alan, gerçekten tür bağımsız değişkenleri kombinasyonu başına bir alan elde edersiniz bildiğin sürece. Benim tahminim bu R# sadece farkında değilsin belki. seni uyarıyor

İşte buna bir örnek:

using System;

public class Generic<T>
{
    // Of course we wouldn't normally have public fields, but...
    public static int Foo;
}

public class Test
{
    public static void Main()
    {
        Generic<string>.Foo = 20;
        Generic<object>.Foo = 10;
        Console.WriteLine(Generic<string>.Foo); // 20
    }
}

, Generic<string>.Foo Generic<object>.Foo - farklı bir alandır gördüğünüz gibi ayrı değerleri tutun.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Absolute Zero(Programming Tutorials)

    Absolute Zer

    22 Kasım 2012
  • bcbauer

    bcbauer

    7 ŞUBAT 2007
  • The Fashion Sight

    The Fashion

    22 AĞUSTOS 2011