Spotify.NET/SpotifyAPI/Web/Util.cs

42 lines
1.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace SpotifyAPI.Web
{
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
{
Enum e = (Enum)(object)en;
IEnumerable<StringAttribute> attributes =
Enum.GetValues(typeof(T))
.Cast<T>()
.Where(v => e.HasFlag((Enum)(object)v))
.Select(v => typeof(T).GetField(v.ToString(CultureInfo.InvariantCulture)))
.Select(f => f.GetCustomAttributes(typeof(StringAttribute), false)[0])
.Cast<StringAttribute>();
2016-03-31 11:08:23 +01:00
List<string> list = new List<string>();
attributes.ToList().ForEach(element => list.Add(element.Text));
return string.Join(separator, list);
}
2017-03-02 21:28:55 +00:00
public static double ToUnixTimeMillisecondsPoly(this DateTime time)
2017-03-02 21:28:55 +00:00
{
return time.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
2017-03-02 21:28:55 +00:00
}
}
public sealed class StringAttribute : Attribute
{
2016-03-31 11:08:23 +01:00
public string Text { get; set; }
2016-03-31 11:08:23 +01:00
public StringAttribute(string text)
{
Text = text;
}
}
}