扩展方法的使用

44 篇文章 0 订阅
2 篇文章 0 订阅
public static class DropDownHelper
{
    public static void PopulateFromEnum<T>(this DropDownList drpist)
    {
        Type enumType = typeof(T);

        if (enumType.BaseType != typeof(Enum))
            throw new ArgumentException("T must be of type System.Enum");

        Array enumValArray = Enum.GetValues(enumType);

        foreach (string s in Enum.GetNames(enumType))
            drpist.Items.Add(new ListItem(s.Replace("_", " "), Convert.ToInt16(Enum.Parse(enumType, s)).ToString()));
    }
}
public static class CheckBoxListHelper
{
    public static void PopulateFromEnum<T>(this CheckBoxList cblist)
    {
        Type enumType = typeof(T);
        if (enumType.BaseType != typeof(Enum))
            throw new ArgumentException("T must be of type System.Enum");
        Array enumValArray = Enum.GetValues(enumType);
        foreach (string s in Enum.GetNames(enumType))
            cblist.Items.Add(new ListItem(s.Replace("_", " "), Convert.ToInt16(Enum.Parse(enumType, s)).ToString()));
    }
}
public static class DateTimeHelper
{
    /// <summary>
    /// Given a datetime object will return a datetime object for the monday of that week.
    /// If argument datetime is a Monday then the return object will be a copy of the input argument.
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static DateTime FindMonday(this DateTime dateTime)
    {
        DateTime rtnDate = dateTime;


        while (rtnDate.DayOfWeek != DayOfWeek.Monday)
            rtnDate = rtnDate.AddDays(-1);


        return rtnDate;
    }


    /// <summary>
    /// Given datetime object returns a datetime object for the first day for the month of the argument datetime
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static DateTime FindFirstOfMonth(this DateTime dateTime)
    {
        DateTime rtnVal = dateTime;


        while (rtnVal.Day != 1)
            rtnVal = rtnVal.AddDays(-1);


        return rtnVal;
    }


    /// <summary>
    /// Return datetime object for the first day of the year for the datetime argument
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static DateTime FindFirstOfYear(this DateTime dateTime)
    {
        DateTime rtnVal = dateTime;


        while (rtnVal.DayOfYear != 1)
            rtnVal = rtnVal.AddDays(-1);


        return rtnVal;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值