简体   繁体   English

问题定制列表<T>进入 ListCustomMinIndexedForEach<T> (从不相关的实现中剥离)

[英]issues customizing List<T> into ListCustomMinIndexedForEach<T> (stripped from irelevant implementations)

I tried to do my best to keep the title short but still informative.我尽力使标题简短但仍能提供信息。

I think I have succeeded with most of it我想我已经成功了大部分

my problem For Now: (TLDR below)我现在的问题:(下面的 TLDR)

I could not succeed implementing a public void method-member, nor an extension to Implement custom ForEach() with index..我无法成功实现 public void 方法成员,也无法使用索引实现自定义ForEach()的扩展。

as Extension作为扩展

    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;
    }

this is a small issue I guess, the objectives :我猜这是一个小问题,目标是

  • Create Customized List No Fancy Extra( Expensive ) methods创建自定义列表没有花哨的额外(昂贵)方法

  • add an elegant ForEach implementation添加一个优雅的 ForEach 实现

  • optional tricks like out of the box GetEnumValues/names .可选的技巧,比如开箱即用的GetEnumValues/names To{ourCustomList}()

USAGE用法

in this example I am using an Enum ( names of Actions also used to present action items in console menu)在这个例子中,我使用了一个Enum (操作的名称也用于在控制台菜单中显示操作项)

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

so I print it to console by unleashing the power of That small List class所以我通过释放那个小的 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");

and it's simply using (TRYING without success for now..)它只是在使用(现在尝试没有成功..)

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;
}

so I started with MSDN List.cs所以我从MSDN List.cs开始

and I tried to implement as less methods as possible我试图尽可能少地实施方法

  • leaving minimal important functionality留下最少的重要功能
  • altering Growth/Expand for -minimal copying so staring capacity is from say 10-50, multiplied by *4 on each limit...更改增长/扩展以进行-最小复制,因此起始容量为 10-50,在每个限制上乘以 *4...

came out with this CODE出来了这个代码

well I came up with this aventually好吧,我最终想到了这个

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;
}

end even better结局更好

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

[1] Red [1] 红色

[2] blue ... .... [2] 蓝色……

..... .....

catch user key and u have a one line console menu抓住用户键,你有一个单行控制台菜单

Why not use some nice LINQ ?为什么不使用一些不错的LINQ呢?

public class Program
{
    public enum ActSrptS { CreateMmfTfomFile_DoFormat, OpenExitMmfT }

    private static void Main()
    {
        var action = ActSrptS.OpenExitMmfT;

        Enum.GetValues(action.GetType())
            .OfType<ActSrptS>()
            .Select((v, i) => $"[{i + 1}] {v}")
            .ToList()
            .ForEach(Console.WriteLine);

        Console.WriteLine("===============\r\n");
        Console.WriteLine("Please select Action");

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM