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
|
|
|
|
|
{
|
2017-05-29 22:17:15 +01: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>();
|
|
|
|
|
|
2016-03-31 11:08:23 +01:00
|
|
|
|
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
|
|
|
|
}
|
2017-03-02 21:28:55 +00:00
|
|
|
|
|
2018-04-01 16:56:47 +01:00
|
|
|
|
public static long ToUnixTimeMillisecondsPoly(this DateTime time)
|
2017-03-02 21:28:55 +00:00
|
|
|
|
{
|
2018-04-01 16:56:47 +01:00
|
|
|
|
return (long)time.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
|
2017-03-02 21:28:55 +00:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2016-03-31 11:08:23 +01:00
|
|
|
|
public string Text { get; set; }
|
2015-10-16 23:44:35 +01:00
|
|
|
|
|
2016-03-31 11:08:23 +01:00
|
|
|
|
public StringAttribute(string text)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
|
|
|
|
Text = text;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-07 12:30:00 +00:00
|
|
|
|
}
|