21 mayo 2018

C# - Generics and Delegates


C# - Generics and Delegates
Método para generar archivo de texto con Generics y Delegates

public class Bo
{
    public static void GenBo()
    {
        #region List Data
        List<Informations> bines = new List<Informations>
        {
        new Informations(1,"604237"),
        new Informations(2,"604238"),
        new Informations(3,"604239"),
        new Informations(4,"604240")
        };
        #endregion
        
        #region Create Directory
        DirectoryInfo directoryInfo = new DirectoryInfo("C:/PR/");
        if (!directoryInfo.Exists) directoryInfo.Create();
        #endregion
        
        #region Write File
        bines.ForEach(delegate (Informations t)
        {
            string filename = directoryInfo + t.Id.ToString() t.Name + ".txt";
            using (FileStream fileStream = new FileStream(filename, 
            FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (StreamWriter p = new StreamWriter(fileStream))
                {
                    p.WriteLine(t.Id.ToString() + t.Name);
                }
            }
        });
        #endregion
    }
}

public class Informations
{
    public int Id { get; set; }
    public string Name { get; set; }

    public Informations(int Id, string Name)
    {
        this.Id = Id;
        this.Name = Name;
    }
}

No hay comentarios: