136 lines
4.4 KiB
C#
136 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.ComponentModel;
|
|
using System.Globalization;
|
|
using System.Configuration;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Web;
|
|
using tyme.solar;
|
|
using tyme.lunar;
|
|
using tyme.sixtycycle;
|
|
|
|
/// <summary>
|
|
/// publicFun 的摘要描述
|
|
/// </summary>
|
|
public class publicFun
|
|
{
|
|
public publicFun()
|
|
{
|
|
//
|
|
// TODO: 在這裡新增建構函式邏輯
|
|
//
|
|
}
|
|
public static Dictionary<int, string> enum_desc<T>()
|
|
{
|
|
//var v1 = typeof(type).GetEnumNames(); // string[] of enum values
|
|
var v2 = typeof(T).GetEnumValues().Cast<int>().ToList(); // int[] of enum values
|
|
//var v3 = typeof(type).GetEnumValues().Cast<type>().ToList(); // type[] of enum values
|
|
var v4 = publicFun.GetDescriptions<T>().ToList(); // string[] of dnum descriptioins
|
|
|
|
//var v5 = Enum<type>.GetNames(); // ==v1
|
|
//var v6 = Enum<type>.GetValues(); // ==v3
|
|
|
|
//var v7 = Enum<type>.GetNames().ToDictionary(x => x, x => (int)Enum<type>.Parse(x)); // v7["Personal"]... int[]
|
|
////var v8 = v7.ToDictionary(x => x.Key, x => Enum<type>.GetAttribute<EnumMemberAttribute>(x.Key).Value); // not work
|
|
|
|
//寫成公用
|
|
var dictionary = new Dictionary<int, string>();
|
|
for (int i = 0; i < v2.Count; i++)
|
|
{
|
|
dictionary.Add(v2[i], v4[i]);
|
|
}
|
|
return dictionary;
|
|
}
|
|
|
|
public static IEnumerable<string> GetDescriptions<T>()
|
|
{
|
|
var attributes = typeof(T).GetMembers()
|
|
.SelectMany(member => member.GetCustomAttributes(typeof(DescriptionAttribute), true)
|
|
.Cast<DescriptionAttribute>())
|
|
.ToList();
|
|
|
|
return attributes.Select(x => x.Description);
|
|
}
|
|
|
|
public static class Enum<T> where T : struct, IComparable, IFormattable, IConvertible
|
|
{
|
|
public static IEnumerable<T> GetValues()
|
|
{
|
|
return (T[])Enum.GetValues(typeof(T));
|
|
}
|
|
public static IEnumerable<string> GetNames()
|
|
{
|
|
return Enum.GetNames(typeof(T));
|
|
}
|
|
|
|
public static T Parse(string @enum)
|
|
{
|
|
return (T)Enum.Parse(typeof(T), @enum);
|
|
}
|
|
|
|
public static S GetAttribute<S>(string @enum) where S : Attribute
|
|
{ // not working
|
|
return (S)typeof(T).GetMember(@enum)[0]
|
|
.GetCustomAttributes(typeof(S), false)
|
|
.SingleOrDefault();
|
|
// var v7 = Enum<type>.GetNames().ToDictionary(x => x, x => (int)Enum<type>.Parse(x)); // v7["Personal"]... int[]
|
|
// var v8 = v7.ToDictionary(x => x.Key, x => Enum<type>.GetAttribute<EnumMemberAttribute>(x.Key).Value);
|
|
}
|
|
|
|
// class EnumConverter
|
|
// https://stackoverflow.com/questions/3816718/how-to-get-an-array-of-all-enum-values-in-c
|
|
// https
|
|
|
|
}
|
|
|
|
public static string chagenDate(DateTime? date)//因為欄位值是nullable, 所以把null的處理拉進函數做
|
|
{ //可改公用
|
|
//農曆年
|
|
if (date == null) return null;
|
|
|
|
DateTime d = date.Value;
|
|
|
|
// 使用 tyme4net 方法
|
|
try
|
|
{
|
|
var solarDay = SolarDay.FromYmd(d.Year, d.Month, d.Day);
|
|
var lunarDay = solarDay.GetLunarDay();
|
|
|
|
int minYear = lunarDay.Year - 1911;
|
|
return $"民{minYear}年/{lunarDay.ToString().Substring(2)}";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// 如果 tyme4net 完全失敗,回退到原本的方法
|
|
try
|
|
{
|
|
ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();
|
|
int yy = chineseDate.GetYear(d) - 1911;
|
|
int mm = chineseDate.GetMonth(d);
|
|
int dd = chineseDate.GetDayOfMonth(d);
|
|
return $"民{yy}年{mm:00}月{dd:00}日";
|
|
}
|
|
catch
|
|
{
|
|
return $"農曆轉換錯誤: {ex.Message}";
|
|
}
|
|
}
|
|
}
|
|
|
|
#region 刪除檔案 for api
|
|
public void DeleteFile(string filePath)
|
|
{
|
|
string path = HttpContext.Current.Server.MapPath(filePath);
|
|
FileInfo FileInfo = new FileInfo(path);
|
|
if (FileInfo.Exists)
|
|
{
|
|
FileInfo.Delete();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
} |