第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

屬性輔助方法的 c# 泛型

屬性輔助方法的 c# 泛型

C#
森林海 2022-12-24 12:55:28
我找不到在 .Net Core 中實現(xiàn)此 DRY 的好方法。(不要重復(fù)自己)。我怎樣才能做到不重復(fù)大部分邏輯?以下是 2 種方法:    public static string GetCategory(this Enum val)    {        CategoryAttribute[] attributes = (CategoryAttribute[])val            .GetType()            .GetField(val.ToString())            .GetCustomAttributes(typeof(CategoryAttribute), false);        return attributes.Length > 0 ? attributes[0].Category : string.Empty;    }    public static string GetDescription(this Enum val)    {        DescriptionAttribute[] attributes = (DescriptionAttribute[])val            .GetType()            .GetField(val.ToString())            .GetCustomAttributes(typeof(DescriptionAttribute), false);        return attributes.Length > 0 ? attributes[0].Description : string.Empty;    }
查看完整描述

3 回答

?
揚帆大魚

TA貢獻1799條經(jīng)驗 獲得超9個贊

我會從這個開始:


public static T GetAttribute<T>(this Enum val)

    where T : Attribute

{

    return (T)val

    .GetType()

    .GetField(val.ToString())

    .GetCustomAttribute(typeof(T), false);

}

將您的方法變成這樣:


public static string GetCategory(this Enum val)

{

    return val.GetAttribute<CategoryAttribute>()?.Category ?? string.Empty;

}



public static string GetDescription(this Enum val)

{

    return val.GetAttribute<DescriptionAttribute>()?.Description ?? string.Empty;

}

可以說你可以做更多的事情來讓那些最終的方法更干一點,但我猜你在這里使用的模式(從屬性中獲取屬性并返回它的值或空字符串)可能不夠普遍值得專門為此創(chuàng)建一個方法。GetAttribute另一方面,該方法可能具有更高的可重用性。


查看完整回答
反對 回復(fù) 2022-12-24
?
牛魔王的故事

TA貢獻1830條經(jīng)驗 獲得超3個贊

您可以使用GetCustomAttribute<T>instead 的通用版本,它可以將代碼簡化到不需要 IMO 的另一個抽象的地方。


public static string GetCategory(this Enum val)

{

    return val.GetType()

          .GetField(val.ToString())

          .GetCustomAttribute<CategoryAttribute>(false)?.Category ?? string.Empty;

}


public static string GetDescription(this Enum val)

{

    return val.GetType()

          .GetField(val.ToString())

          .GetCustomAttribute<DescriptionAttribute>(false)?.Description ?? string.Empty;

}



查看完整回答
反對 回復(fù) 2022-12-24
?
波斯汪

TA貢獻1811條經(jīng)驗 獲得超4個贊

在 C# 7.3 中,您可以將方法限制為枚舉類型。這將為您節(jié)省一箱枚舉。


public static class AttributeExtensions

{

    public static string GetCategory<T>(this T val) where T: Enum

    {

        return GetAttr<CategoryAttribute, T>(val)?.Category ?? "";

    }


    public static string GetDescription<T>(this T val) where T : Enum

    {

        return GetAttr<DescriptionAttribute, T>(val)?.Description ?? "";

    }


    private static TAttr GetAttr<TAttr, T>(this T val) where TAttr : Attribute

    {

        return (TAttr)typeof(T)

            .GetField(val.ToString())

            ?.GetCustomAttributes(typeof(TAttr), false)

            ?.FirstOrDefault();

    }

}

此外,在使用反射時,緩存性能很重要:


public static class AttributeExtensions

{

    private class EnumMetadata

    {

        public CategoryAttribute CategoryAttribute { get; set; }

        public DescriptionAttribute DescriptionAttribute { get; set; }

    }


    private class EnumMetadataCache<T> where T : Enum

    {

        private static readonly ConcurrentDictionary<T, EnumMetadata> MetadataCache = new ConcurrentDictionary<T, EnumMetadata>();


        public static EnumMetadata GetMetadata(T item)

        {

            return MetadataCache.GetOrAdd(item, val =>

                new EnumMetadata

                {

                    CategoryAttribute = GetAttr<CategoryAttribute, T>(val),

                    DescriptionAttribute = GetAttr<DescriptionAttribute, T>(val)

                }

            );

        }

    }


    public static string GetCategory<T>(this T val) where T : Enum

    {

        return EnumMetadataCache<T>.GetMetadata(val).CategoryAttribute?.Category ?? "";

    }


    public static string GetDescription<T>(this T val) where T : Enum

    {

        return EnumMetadataCache<T>.GetMetadata(val).DescriptionAttribute?.Description ?? "";

    }


    private static TAttr GetAttr<TAttr, T>(this T val) where TAttr : Attribute

    {

        return (TAttr)typeof(T)

            .GetField(val.ToString())

            ?.GetCustomAttributes(typeof(TAttr), false)

            ?.FirstOrDefault();

    }

}


查看完整回答
反對 回復(fù) 2022-12-24
  • 3 回答
  • 0 關(guān)注
  • 101 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號