public enum BBCodes {
[Description("b")]
Bold = 1,
[Description("i")]
Italic = 2
}
public static class BBCodesExt {
public static string Wrap(this BBCodes code, string contents) {
return $"[{code.ToStringDescription()}]{contents}[/{code.ToStringDescription()}]";
}
public static string ToStringDescription(this BBCodes code) {
var attributes = (DescriptionAttribute[])code.GetType().GetField(code.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : "";
}
}
Veja funcionando no dotNetFiddle.
public sealed class BBCodes {
public static readonly string Bold = "b";
public static readonly string Italic = "i";
//...
private string code;
public BBCodes(string code) {
this.code = code;
}
public string Wrap(string contents)
{
return $"[{code}]{contents}[/{code}]";
}
}
Veja funcionando no dotNetFiddle.