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

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

按屬性值查找類屬性

按屬性值查找類屬性

C#
天涯盡頭無女友 2021-11-14 15:55:07
我想通過其屬性和屬性值查找類屬性。鑒于此屬性和類:class MyAttribute : Attribute{    public MyAttribute(string name)    {        Name = name;    }    public string Name { get; set; }}class MyClass{    [MyAttribute("Something1")]    public string Id { get; set; }    [MyAttribute("Something2")]    public string Description { get; set; } }我知道我可以找到這樣的特定屬性:var c = new MyClass();var props = c.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(MyAttribute)));但是如何過濾屬性名稱值“Something2”?所以我的最終目標(biāo)是通過在 MyClass 中搜索值為“Something”的屬性 MyAttribute 來輸出“MyClass.Description”。
查看完整描述

2 回答

?
呼喚遠(yuǎn)方

TA貢獻(xiàn)1856條經(jīng)驗 獲得超11個贊

你也可以做這樣的事情

var c = new MyClass();var props = c.GetType()
             .GetProperties()
             .Where(prop => prop.GetCustomAttributes(false)
                                .OfType<MyAttribute>()
                                .Any(att => att.Name == "Something1"));


查看完整回答
反對 回復(fù) 2021-11-14
?
牧羊人nacy

TA貢獻(xiàn)1862條經(jīng)驗 獲得超7個贊

以舊的 foreach 風(fēng)格


var c = new MyClass();

var props = c.GetType().GetProperties()

    .Where(prop => Attribute.IsDefined(prop, typeof(MyAttribute)));


foreach (var prop in props)

{

    MyAttribute myAttr = (MyAttribute)Attribute.GetCustomAttribute(prop, typeof(MyAttribute));

    if (myAttr.Name == "Something2")

        break; //you got it

}


查看完整回答
反對 回復(fù) 2021-11-14
  • 2 回答
  • 0 關(guān)注
  • 156 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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