-3
属性リストがありますが、各属性の特定のプロパティを取得したいとします。 examleのために、私はSystem.Attributeをルート属性リフレクションとして使用する
[HttpGet, Route("/autocomplete")]
[ActionInfo(Description = "bla bla bla blac")]
// GET: AutoComplete
public ActionResult AutoComplete()
{
return View();
}
private static IEnumerable<Attribute> MyMethod(IEnumerable<Attribute> attributes)
{
foreach (Attribute attribute in attributes)
{
switch (attribute.GetType().Name)
{
case "HttpGetAttribute":
{
using (attribute as HttpGetAttribute)
{
// my business
}
break;
}
case "RouteAttribute":
{
using (attribute as RouteAttribute)
{
// my business
}
break;
}
case "ActionInfoAttribute":
{
using (attribute as ActionInfoAttribute)
{
// my business
}
break;
}
}
}
return null;
}
C#7の 'if(属性はHttpGetAttributeがキャストされている) –