demo20230512/Models/ExtensionMethods/Extension.cs

49 lines
1.6 KiB
C#
Raw Normal View History

2023-05-12 10:20:28 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Reflection;
namespace Weee.Models.ExtensionMethods
{
public static class Extension
{
// to use this function , pease make sure all the enum value have a display attribute
public static string DisplayString(this Enum enumValue)
{
var enumType = enumValue.GetType();
var Resource = enumType.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
.ResourceType;
var name = enumType.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
.Name;
return Resource.GetProperty(name).GetValue(name).ToString();
}
public static int Months(this LCA LCAValue)
{
return LCAValue.EndDate.Month - LCAValue.StartDate.Month + 1 + 12 * (LCAValue.EndDate.Year - LCAValue.StartDate.Year);
}
public static string MonthLabel(this MonthlyData Value)
{
if ((Value.StartBase + Value.Index) % 12 == 0) return "12";
return ((Value.StartBase + Value.Index) % 12).ToString();
}
public static decimal Multiply(this IEnumerable<decimal> self)
{
decimal result = 1m;
foreach (var item in self)
{
result =result* item;
}
return result;
}
}
}