2014-07-20 21:42:46 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
using System.Globalization;
|
2014-07-20 21:42:46 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
namespace SpotifyAPI.Web
|
2014-07-20 21:42:46 +01:00
|
|
|
|
{
|
|
|
|
|
public static class Util
|
|
|
|
|
{
|
2015-02-12 23:23:07 +00:00
|
|
|
|
public static string GetStringAttribute<T>(this T en, String separator) where T : struct, IConvertible
|
2014-07-20 21:42:46 +01:00
|
|
|
|
{
|
2015-02-12 23:23:07 +00:00
|
|
|
|
Enum e = (Enum)(object)en;
|
2014-07-20 21:42:46 +01:00
|
|
|
|
IEnumerable<StringAttribute> attributes =
|
2015-02-12 23:23:07 +00:00
|
|
|
|
Enum.GetValues(typeof(T))
|
|
|
|
|
.Cast<T>()
|
|
|
|
|
.Where(v => e.HasFlag((Enum)(object)v))
|
2015-07-07 17:11:11 +01:00
|
|
|
|
.Select(v => typeof(T).GetField(v.ToString(CultureInfo.InvariantCulture)))
|
2014-07-20 21:42:46 +01:00
|
|
|
|
.Select(f => f.GetCustomAttributes(typeof(StringAttribute), false)[0])
|
|
|
|
|
.Cast<StringAttribute>();
|
|
|
|
|
|
|
|
|
|
List<String> list = new List<String>();
|
2015-07-07 17:11:11 +01:00
|
|
|
|
attributes.ToList().ForEach(element => list.Add(element.Text));
|
2015-12-07 12:30:00 +00:00
|
|
|
|
return string.Join(separator, list);
|
2014-07-20 21:42:46 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-07 17:11:11 +01:00
|
|
|
|
|
2015-10-16 23:31:01 +01:00
|
|
|
|
public sealed class StringAttribute : Attribute
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
|
|
|
|
public String Text { get; set; }
|
2015-10-16 23:44:35 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
public StringAttribute(String text)
|
|
|
|
|
{
|
|
|
|
|
Text = text;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-07 12:30:00 +00:00
|
|
|
|
}
|