c# - 将 List<T> 定制为 ListCustomMinIndexedForEach<T> 的问题(从不相关的实现中剥离)

标签 c# performance collections customization extension-methods

我尽力使标题简短但内容丰富。

我想我已经成功了大部分

我现在的问题:(下面的 TLDR)

我无法成功实现 public void 方法成员,也无法成功实现带有索引的自定义 ForEach() 的扩展..

作为扩展

    public static ListWithCounter<T> ForEach<T>(this  ListWithCounter<T> Self, Action<T> itm)//, ListWithCounter<T> l = l.CurId)
    {

        for (int i = 0; i < Self.Count; i++)
        {
            Self.CurId = i;
            itm(Self[i]);Self.CurId++;

        }
        return Self;
    }

我猜这是一个小问题,目标:

  • 创建自定义列表没有花哨的额外(昂贵)方法

  • 添加一个优雅的 ForEach 实现

  • 开箱即用的可选技巧 GetEnumValues/names . To{ourCustomList}()

用法

在这个例子中,我使用了一个Enum(Actions 的名称也用于在控制台菜单中显示操作项)

public enum ActSrptS { CreateMmfTfomFile_DoFormat, OpenExitMmfT, .... }

所以我通过释放那个小列表类的力量将它打印到控制台

                                    //a struct ConsoleModifiers + ConsoleKey
ActScrptS aAction = ActScrptS._Start; Combination Comb = new Combination();
var actS = aAction._EnmGetValues();
var actSNms = aAction.EnumGetNamesToList().ForEach(Act =>
{
   Console.WriteLine("[{0}]{1}", actS.CurId, Act);
});
Console.WriteLine("===============\r\n");
Console.WriteLine("please Select Action");

它只是简单地使用(暂时没有成功尝试..)

public static ListWithCounter<string> EnumGetNamesToList(this Enum selfEnum)
{
    return selfEnum.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)
            .Select(f=>f.Name).ToList();
        //var values = Enum.GetNames(typeof(selfEnum)).ToList();
            //return values;
        //var values = Enum.GetValues(typeof(Environment.SpecialFolder)).Cast<Environment.SpecialFolder>().ToList();

}



public static ListWithCounter<Enum> _EnmGetValues(this Enum Self)
{
    ListWithCounter<Enum> enumerations = new ListWithCounter<Enum>();
    foreach (FieldInfo fieldInfo in Self.GetType().GetFields(
              BindingFlags.Static | BindingFlags.Public))
        {
            enumerations.Add((Enum)fieldInfo.GetValue(Self));
        }
    return enumerations;
}

所以我从 MSDN 开始List.cs

并且我尝试实现尽可能少的方法

  • 保留最少的重要功能
  • 改变 Growth/Expand 以实现最小复制,因此起始容量为 10-50,每个限制乘以 *4...

出来了这个CODE

最佳答案

这个是我想出来的

var actS = aActionCur._EnmGetValues().GetAsActionList();


actS.ForEach(act => Console.WriteLine("action [{0}] {1}", actS.CurId, act));
//which is using....v
public static ListWithCounter<Enum> _EnmGetValues(this Enum Self)
{
    ListWithCounter<Enum> enumerations = new ListWithCounter<Enum>();
    foreach (FieldInfo fieldInfo in Self.GetType().GetFields(
              BindingFlags.Static | BindingFlags.Public))
    {
            enumerations.Add((Enum)fieldInfo.GetValue(Self));
    }
        return enumerations;
}


//which is using....v
public static ListWithCounter<T> ForEach<T>(this  ListWithCounter<T> Self, Action<T> itm)
{
        for (int i = 0; i < Self.Count; i++)
        {

            itm(Self[i]); Self.CurId++;
        }
        return Self;
}

更好的结局

    public static ListWithCounter<ConsoleColor> GetAsConColors(this ListWithCounter<Enum> self)
    {
        return self[0].GetType().GetEnumValues().Cast<ConsoleColor>().ToList();
    }

[1] 红色

[2] 蓝... ....

.....

捕获用户 key ,你有一个单行控制台菜单

关于c# - 将 List<T> 定制为 ListCustomMinIndexedForEach<T> 的问题(从不相关的实现中剥离),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35536545/

相关文章:

c# - 在模型更改时更新数据库架构而不会丢失数据

c# - OpenCVsharp:实时流式传输

c# - 如何在 SoapHttpClientProtocol 中设置 User-Agent header ?

performance - 是否可以为 RTL 创建基于硬件的合成器?

java - 获取 map 中最后一行的数据

collections - 计算 Landsat 集合的 NDVI

c# - 获取 Windows 机器上的 PC(系统)信息

C++ - 执行速度测试的正确方法是什么?

sql-server - 在 View 中使用调用 GETDATE() 的函数是否始终比直接使用 GETDATE() 提供更差的性能?

java - 关于不可变列表(由 Arrays.asList() 创建)