简体   繁体   中英

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)

I could not succeed implementing a public void method-member, nor an extension to Implement custom ForEach() with index..

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

  • optional tricks like out of the box 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)

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

so I print it to console by unleashing the power of That small List class

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

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

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

[2] blue ... ....

.....

catch user key and u have a one line console menu

Why not use some nice 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");

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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