将List <t>自定义为ListCustomMinIndexedForEach <t>(从相关实现中剥离)

时间:2016-02-21 13:09:32

标签: c# performance collections customization extension-methods

我尽力保持标题简短但仍然提供信息。

我认为我已经取得了大部分成功

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

我无法成功实现public void method-member,也无法实现带有索引的自定义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}()

USAGE

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

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

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

                                    //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

开始

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

  • 留下最小的重要功能
  • 改变生长/扩展 - 最小复制,因此凝视容量从10-50开始,乘以每个限制的* 4 ......

发布了这个CODE

2 个答案:

答案 0 :(得分:0)

我最近想出了这个

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]蓝色...... ....

.....

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

答案 1 :(得分:0)

为什么不使用一些不错的h3 { clear: left; } .box { float: left; clear: left; height: 100px; width: 200px; background: green; }

<div id="main">
  <h3>
    This is a heading.
  </h3>
  <div class="box">
    box 1
  </div>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas culpa voluptas quod eveniet voluptatem rerum quasi voluptates, assumenda itaque inventore maiores. Exercitationem nam quia id voluptatum, itaque quisquam eligendi ullam.
  </p>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas culpa voluptas quod eveniet voluptatem rerum quasi voluptates, assumenda itaque inventore maiores. Exercitationem nam quia id voluptatum, itaque quisquam eligendi ullam.
  </p>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas culpa voluptas quod eveniet voluptatem rerum quasi voluptates, assumenda itaque inventore maiores. Exercitationem nam quia id voluptatum, itaque quisquam eligendi ullam.
  </p>
  <h3>
    Another heading.
  </h3>
  <p>
    This is the text I want to move and be just like the text next to box 1. This is the text I want to move and be just like the text next to box 1. This is the text I want to move and be just like the text next to box 1. This is the text I want to move and be just like the text next to box 1. 
  </p>
  <div class="box">
    box 2
  </div>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas culpa voluptas quod eveniet voluptatem rerum quasi voluptates, assumenda itaque inventore maiores. Exercitationem nam quia id voluptatum, itaque quisquam eligendi ullam.
  </p>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas culpa voluptas quod eveniet voluptatem rerum quasi voluptates, assumenda itaque inventore maiores. Exercitationem nam quia id voluptatum, itaque quisquam eligendi ullam.
  </p>
</div>