첫 등록
This commit is contained in:
72
BitgetApi/Utils/BaseSingleton.cs
Normal file
72
BitgetApi/Utils/BaseSingleton.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace BitgetAPI.Utils
|
||||
{
|
||||
public class BaseSettings<T> where T : class, new()
|
||||
{
|
||||
private static readonly object lockObject = new object();
|
||||
private static readonly object fileLockObject = new object();
|
||||
private static T _instance;
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (lockObject)
|
||||
{
|
||||
//var path = GetSettingFilePath();
|
||||
|
||||
if (null == _instance)
|
||||
_instance = CreateInstance();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
protected static T CreateInstance()
|
||||
{
|
||||
try
|
||||
{
|
||||
Type type = typeof(T);
|
||||
var saveFilePath = getFilePath();
|
||||
|
||||
if (File.Exists(saveFilePath))
|
||||
{
|
||||
//File.ReadAllText(saveFilePath);
|
||||
return JsonConvert.DeserializeObject<T>(File.ReadAllText(saveFilePath)) ?? new T();
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return new T();
|
||||
}
|
||||
|
||||
private static string getFilePath()
|
||||
{
|
||||
return typeof(T).Name + ".json";
|
||||
}
|
||||
|
||||
~BaseSettings()
|
||||
{
|
||||
SaveConfig();
|
||||
}
|
||||
|
||||
public void SaveConfig()
|
||||
{
|
||||
lock (fileLockObject)
|
||||
{
|
||||
try
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(this, Formatting.Indented);
|
||||
File.WriteAllText(getFilePath(), json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
42
BitgetApi/Utils/Convertor.cs
Normal file
42
BitgetApi/Utils/Convertor.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BitgetApi.Utils
|
||||
{
|
||||
public static class Convertor
|
||||
{
|
||||
/// <summary>
|
||||
/// Get Enum Description
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetEnumDescription(Enum source)
|
||||
{
|
||||
FieldInfo fi = source.GetType().GetField(source.ToString());
|
||||
var att = (DescriptionAttribute)fi.GetCustomAttribute(typeof(DescriptionAttribute));
|
||||
if (att != null)
|
||||
{
|
||||
return att.Description;
|
||||
}
|
||||
else
|
||||
{
|
||||
return source.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public static long GetDateTimeToUnixtimeStamp(DateTime datetime)
|
||||
{
|
||||
return (long)(datetime - new DateTime(1970, 1, 1, 9, 0, 0)).TotalSeconds;
|
||||
}
|
||||
|
||||
public static DateTime GetUnixtimeStampToDateTime(long unixStamptime)
|
||||
{
|
||||
return new DateTime(1970, 1, 1, 9, 0, 0).AddMilliseconds(unixStamptime); ;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user