Как GetType (). GetFields с настраиваемым атрибутом?

этот старый код возвращает список полей, украшенных атрибутом в вызове метода с использованием отражения

Есть ли способ заменить его TypeDescripter или LINQ?

    public static FieldInfo[] GetFieldsWithAttribute(Type type, Attribute attr, bool onlyFromType)
    {
        System.Reflection.FieldInfo[] infos = type.GetFields();
        int cnt = 0;
        foreach (System.Reflection.FieldInfo info in infos)
        {
            if (info.GetCustomAttributes(attr.GetType(), false).Length > 0)
            {
                if (onlyFromType && info.DeclaringType != type)
                    continue;

                cnt++;
            }
        }

        System.Reflection.FieldInfo[] rc = new System.Reflection.FieldInfo[cnt];
        // now reset !
        cnt = 0;

        foreach (System.Reflection.FieldInfo info in infos)
        {
            if (info.GetCustomAttributes(attr.GetType(), false).Length > 0)
            {
                if (onlyFromType && info.DeclaringType != type)
                    continue;

                rc[cnt++] = info;
            }
        }

        return rc;
    }
5
задан Kumar 1 December 2011 в 19:32
поделиться