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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何通過(guò)反射獲取類(lèi)型的所有常量?

如何通過(guò)反射獲取類(lèi)型的所有常量?

倚天杖 2019-10-19 16:55:33
如何使用反射獲取任何類(lèi)型的所有常量?
查看完整描述

3 回答

?
人到中年有點(diǎn)甜

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

雖然這是一個(gè)舊代碼:


private FieldInfo[] GetConstants(System.Type type)

{

    ArrayList constants = new ArrayList();


    FieldInfo[] fieldInfos = type.GetFields(

        // Gets all public and static fields


        BindingFlags.Public | BindingFlags.Static | 

        // This tells it to get the fields from all base types as well


        BindingFlags.FlattenHierarchy);


    // Go through the list and only pick out the constants

    foreach(FieldInfo fi in fieldInfos)

        // IsLiteral determines if its value is written at 

        //   compile time and not changeable

        // IsInitOnly determines if the field can be set 

        //   in the body of the constructor

        // for C# a field which is readonly keyword would have both true 

        //   but a const field would have only IsLiteral equal to true

        if(fi.IsLiteral && !fi.IsInitOnly)

            constants.Add(fi);           


    // Return an array of FieldInfos

    return (FieldInfo[])constants.ToArray(typeof(FieldInfo));

}

資源


您可以使用泛型和LINQ輕松將其轉(zhuǎn)換為更清晰的代碼:


private List<FieldInfo> GetConstants(Type type)

{

    FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Public |

         BindingFlags.Static | BindingFlags.FlattenHierarchy);


    return fieldInfos.Where(fi => fi.IsLiteral && !fi.IsInitOnly).ToList();

}

或一行:


type.GetFields(BindingFlags.Public | BindingFlags.Static |

               BindingFlags.FlattenHierarchy)

    .Where(fi => fi.IsLiteral && !fi.IsInitOnly).ToList();


查看完整回答
反對(duì) 回復(fù) 2019-10-19
  • 3 回答
  • 0 關(guān)注
  • 854 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號(hào)

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