Last active
March 9, 2020 23:24
-
-
Save gdonega/4b5d2c9eb7fa182e44f8c334b1f5e7cb to your computer and use it in GitHub Desktop.
C# - Enum: TiposJogosComSwitch- Atributo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using TiposJogosComAtributo.Atributos; | |
namespace TiposJogosComAtributo | |
{ | |
public enum TipoJogo | |
{ | |
[Descricao("Ação")] | |
ACAO, | |
[Descricao("Aventura")] | |
AVENTURA, | |
[Descricao("Role-playing game")] | |
RPG | |
} | |
public static class TipoFilmeDescricao | |
{ | |
public static string GetDescricao(this TipoJogo tipoJogo) | |
{ | |
// Recupera o tipo da enum | |
var tipoJogoType = typeof(TipoJogo); | |
var valorDescricao = tipoJogoType | |
.GetMember(tipoJogo.ToString()).FirstOrDefault() // Recupera ACAO, AVENTURA ou RPG de acordo com a instancia | |
.GetCustomAttributes(typeof(Descricao), false) // Recupera o atributo Descricao | |
.Select(x => (Descricao)x) // Faz o cast | |
.FirstOrDefault() // Recupera o primeiro objeto | |
.Valor // Recupera o valor Ação, Aventura ou Role-playing game | |
; | |
// Retorna o valor | |
return valorDescricao; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment