migrate to new git
This commit is contained in:
125
web/App_Code/publicFun.cs
Normal file
125
web/App_Code/publicFun.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
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;
|
||||
|
||||
/// <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的處理拉進函數做
|
||||
{ //可改公用
|
||||
//農曆年
|
||||
ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();
|
||||
try
|
||||
{
|
||||
if (date != null)
|
||||
{
|
||||
DateTime d = date ?? DateTime.Now;
|
||||
int yy = chineseDate.GetYear(d) - 1911;
|
||||
int mm = chineseDate.GetMonth(d);
|
||||
int dd = chineseDate.GetDayOfMonth(d);
|
||||
string dstr = $"民{yy}年{mm:00}月{dd:00}日";
|
||||
return dstr;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ //try catch可能會很慢, 已知條件先判斷
|
||||
//return null;
|
||||
}
|
||||
return null;
|
||||
//民國年
|
||||
//System.Globalization.TaiwanCalendar tc = new System.Globalization.TaiwanCalendar();
|
||||
// tc.GetYear(d), tc.GetMonth(d), tc.GetDayOfMonth(d)
|
||||
}
|
||||
|
||||
#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
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user