ExtensionHelper扩展帮助类

public static class ExtensionHelper{}

字符串转换成指定类型的值

using System.ComponentModel;
/// <summary>
/// 字符串转换成指定类型的值
/// </summary>
/// <typeparam name="T">指定类型</typeparam>
/// <param name="input">字符串</param>
/// <returns>类型的值</returns>
public static T Value<T>(this string input)
{
    try
    {
        if (string.IsNullOrWhiteSpace(input) && typeof(T) != typeof(string))
        {
            return default;
        }
        return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(input);
    }
    catch
    {
        // 转换异常会增加耗时
        return default;
    }
}

DataGrid扩展

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
    if (row != null)
    {
        var presenter = WSCommFunc.GetVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(row);
        if (presenter == null)
        {
            grid.ScrollIntoView(row, grid.Columns[column]);
            presenter = WSCommFunc.GetVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(row);
        }
        return (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
    }
    return null;
}

public static DataGridCell GetCell(this DataGrid grid, int row, int column)
{
    DataGridRow rowContainer = grid.GetRow(row);
    return grid.GetCell(rowContainer, column);
}
public static DataGridRow GetRow(this DataGrid grid, int index)
{
    if (index < 0)
    {
        return null;
    }
    var row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    if (row == null)
    {
        // 可能已虚拟化,进入视图并重试
        grid.UpdateLayout();
        grid.ScrollIntoView(grid.Items[index]);
        row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    }
    return row;
}

BindingList

/// <summary>
/// 添加指定集合到列表的结尾
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="collection"></param>
/// <remarks>只响应<see cref="ListChanged"/>一次</remarks>
public static void AddRange<T>(this BindingList<T> list, IEnumerable<T> collection)
{
    if (collection == null)
    {
        throw new ArgumentNullException(nameof(collection));
    }
    var oldRaiseEventsValue = list.RaiseListChangedEvents;
    try
    {
        list.RaiseListChangedEvents = false;
        foreach (var item in collection)
        {
            list.Add(item);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        list.RaiseListChangedEvents = oldRaiseEventsValue;
        if (list.RaiseListChangedEvents)
        {
            list.ResetBindings();
        }
    }
}
/// <summary>
/// 删除指定集合到列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="collection"></param>
/// <remarks>只响应<see cref="ListChanged"/>一次</remarks>
public static void DeleteRange<T>(this BindingList<T> list, IEnumerable<T> collection)
{
    if (collection == null)
    {
        throw new ArgumentNullException(nameof(collection));
    }
    var oldRaiseEventsValue = list.RaiseListChangedEvents;
    try
    {
        list.RaiseListChangedEvents = false;
        foreach (var item in collection)
        {
            list.Remove(item);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        list.RaiseListChangedEvents = oldRaiseEventsValue;
        if (list.RaiseListChangedEvents)
        {
            list.ResetBindings();
        }
    }
}
扩展方法 解释 入口
TraceMessage Caller Information Attributes调用者信息特性 demo
RemoveAll IList清空 demo
OnUIThread 多线程更新GUI demo
DeepCopy DeepCopyByExpressTree 利用表达式树实现深复制 demo
GetAggregateExceptionMsg 异步捕获包含多个异常的AggregateException demo
posted @ 2021-05-11 15:25  wesson2019  阅读(73)  评论(0编辑  收藏  举报