Как может получить все атрибуты на родословной свойства interface/basetype?

Очень хакерская попытка с xaml, и вы все еще можете заметить артефакты на границах, но для быстрого ознакомления почему бы и нет ..

    

        
        
        
        
        

        
            
                
        

    

enter image description here

5
задан Dane O'Connor 7 November 2008 в 20:03
поделиться

2 ответа

это - проблема платформы. Интерфейсные атрибуты проигнорированы GetCustomAttributes., см. комментарий к этому сообщению в блоге http://hyperthink.net/blog/getcustomattributes-gotcha/#comment-65

1
ответ дан 15 December 2019 в 06:38
поделиться
object[] SomeMagic (PropertyInfo property)
{
    return property.GetCustomAttributes(true);
}

ОБНОВЛЕНИЕ:

Так как мой выше ответа не работает, почему не попробовать что-то вроде этого:

public void Should_Use_Magic_To_Get_CustomAttributes_From_Ancestry()
{

    Assert.AreEqual(checkAttributeCount (typeof (Sedan), "TurningRadious"), 3);
}


int checkAttributeCount (Type type, string propertyName)
{
        var attributesCount = 0;

        attributesCount += countAttributes (type, propertyName);
        while (type.BaseType != null)
        {
            type = type.BaseType;
            attributesCount += countAttributes (type, propertyName);
        }

        foreach (var i in type.GetInterfaces ())
            attributesCount += countAttributes (type, propertyName);
        return attributesCount;
}

int countAttributes (Type t, string propertyName)
{
    var property = t.GetProperty (propertyName);
    if (property == null)
        return 0;
    return (property.GetCustomAttributes (false).Length);
}
2
ответ дан 15 December 2019 в 06:38
поделиться
Другие вопросы по тегам:

Похожие вопросы: