문제

I have an MVC4 app with multiple controllers that all inherit from BaseController. In some of these controllers, I have Actions with a Custom Attribute of PageKey. Page Key has two properties (Group & Key).

I need a way to get every instance of this attribute in all controllers and access the values. I also want to not have to modify the code later when I add new controllers or instances of the attribute.

도움이 되었습니까?

해결책

Ok, got it.

var attributes = Assembly.GetExecutingAssembly()
            .GetTypes()
            .SelectMany(t => t.GetMethods())
            .SelectMany(m => m.GetCustomAttributes())
            .Where(a => a.GetType() == typeof(PageKeyAttribute));

Thanks to Alexei Levenkov.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top