csharp 调用commant类实例并执行它。

调用commant类实例并执行它。

Invoker.cs
namespace AppName.LogicaDeNegocio.Comando
{
    public class Invoker
    {
        private Comando _comando;

        public void SetComando(Comando comando)
        {
            this._comando = comando;
        }

        public void EjecutarComando()
        {
            _comando.Ejecutar();
        }
    }
}

csharp 模式命令子类

模式命令子类

CommandChild.cs
using AppName.Entidades;

namespace AppName.LogicaDeNegocio.Command
{
    public class ComandoConsultarEstado : CommandParent
    {
        public ComandoConsultarEstado(Entidad recibidor) : base(recibidor) { }

         public override void Ejecutar()
         {
             Recibidor= FabricaDao.CrearFabricaDeDao(1).CrearDaoSqlServerEstado().Consultar(Recibidor);
         }
    }
}

csharp C#模式命令父抽象类

C#模式命令父抽象类

CommandParent.cs
using AppName.Entidades;

namespace AppName.LogicaDeNegocio.CommandParent
{
    public abstract class CommandParent
    {
        protected Entidad Recibidor;

        protected CommandParent(Entidad recibidor)
        {
            Recibidor = recibidor;
        }

        public abstract void Ejecutar();
    }
}

csharp 抽象工厂实现子类。

抽象工厂实现子类。

AbstractFactoryDaoChild.cs
namespace AppName.DAO.FabricaDao
{
    public class AbstractFactoryDaoChild : AbstractFactory
    {
        private static  AbstractFactory _fabricaDaoSqlServer;

        private AbstractFactoryDaoChild() { }

        public static FabricaDao GetInstacia()
        {
            if(_fabricaDaoSqlServer == null)
            {
                _fabricaDaoSqlServer = new FabricaDaoSqlServer();
            }
            return _fabricaDaoSqlServer;
        }

        public override DaoSqlServer.DaoSqlServer CrearDaoSqlServerEstado()
        {
            return new DaoSqlServerEstado();
        }
    }

csharp C#模式抽象工厂类。

C#模式抽象工厂类。

AbstractFactory.cs
namespace AppName.DAO.AbstractFactory
{
    public abstract class AbstractFactory
    {
        //Crea La fabrica dependiendo del manejador de base de datos
        public static AbstractFactory CrearFabricaDeDao(int tipoFabrica)
        {
            switch (tipoFabrica)
            {
                case 1: return FabricaDaoSqlServer.GetInstacia();
                //case 2: return FabricaDaoOracle.getInstancia();
                //case 3: return FabricaDaoMySql.getInstancia();
                //case 4: return FabricaDaoPostgre.getInstancia();
                default: return null;
            }
        }
        public abstract DaoSqlServer.AbstractDaoParent CrearDaoSqlServerEstado();
        public abstract DaoSqlServer.AbstractDaoParent CrearDaoSqlServerFilial();
    }
}

csharp C#Dao儿童班

C#Dao儿童班

DaoChild.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using AppName.DAO.ExcepcionesDao;
using AppName.Entidades;

namespace AppName.DAO.DaoSqlServer
{
    public class DaoChild : AbstractDaoParent
    {
        private SqlCommand _cmd;
        private SqlDataReader _rdr;

        public override Entidad Agregar(Entidad entidad)
        {
            throw new NotImplementedException();
        }
        public override Entidad Modificar(Entidad entidad)
        {
            throw new NotImplementedException();
        }
        public override Entidad Consultar(Entidad estado)
        {
            var listaEstado = new List<Entidad>();
            try
            {
                AbrirConexion();
                _cmd = new SqlCommand("dbo.uSpselConsultarEstado", ObjConexion)
                {
                    CommandType = CommandType.StoredProcedure,
                    CommandTimeout = 10
                };
                _rdr = _cmd.ExecuteReader();

                while (_rdr.Read())
                {
                    Entidad elEstado = new Estado();

                    elEstado.Id = int.Parse(_rdr["CodEstado"].ToString());
                    (elEstado as Estado).ElEstado = _rdr["Estado"].ToString();
                    (elEstado as Estado).Razon = _rdr["Razon"].ToString();
                    elEstado.Valid = true;
                    listaEstado.Add(elEstado);
                }
                _rdr.Close();
                estado.Lista = listaEstado;
                estado.Valid = true;
            }
            catch (NullReferenceException ex)
            {
                throw new DatosException("Error nombre o clave nula", ex);

            }
            catch (SqlException ex)
            {
                throw new DatosException("Error en la consulta a Base de datos", ex);

            }
            catch (Exception ex)
            {
                throw new DatosException("Error General", ex);

            }
            finally
            {
                CerrarConexion();
            }
            return estado;
        }
        public override Entidad Eliminar(Entidad entidad)
        {
            throw new NotImplementedException();
        }
    }
}

csharp C#dao模式,抽象父类。

C#dao模式,抽象父类。

AbstractDaoParent.cs
using System;
using System.Configuration;
using System.Data.SqlClient;
using AppName.DAO.ExcepcionesDao;
using AppName.Entidades;

namespace AppName.DAO.DaoSqlServer
{
    public abstract class AbstractDaoParent
    {
        #region atributes
        private readonly string _strConexion;
        public static SqlConnection ObjConexion { get; private set; }
        #endregion

        #region constructor
        protected DaoSqlServer()
        {
            try
            {
                ObjConexion = null;
                _strConexion = ConfigurationManager.ConnectionStrings["Italviajes"].ToString();
            }
            catch (NullReferenceException ex)
            {
                throw new DatosException("Error en el string de conexion", ex);
            }
            catch (Exception ex)
            {
                throw new DatosException("Error General", ex);
            }
        }
        #endregion

        #region methods
        public void AbrirConexion()
        {
            if (!String.IsNullOrEmpty(_strConexion))
            {
                ObjConexion = new SqlConnection(_strConexion);
                ObjConexion.Open();

                if (ObjConexion.State.ToString() != "Open")
                {
                    while (ObjConexion.State.ToString() != "Open")
                    {
                    }
                }
            }
        }
        public void CerrarConexion()
        {
            if (ObjConexion != null)
            {
                if (ObjConexion.State.ToString() == "Open")
                {
                    ObjConexion.Close();
                    ObjConexion.Dispose();
                }
            }
        }

        #endregion

        #region abstractMethods
        public abstract Entidad Agregar(Entidad entidad);
        public abstract Entidad Consultar(Entidad entidad);
        public abstract Entidad Modificar(Entidad entidad);
        public abstract Entidad Eliminar(Entidad entidad);
        #endregion
    }
}

csharp chefOdd.cs

chefOdd.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChefOdd
{
    class Program
    {
        static void Main(string[] args)
        {
            
            var qty = Convert.ToInt32(Console.ReadLine());
            var testCases = new int[qty];
            
            
            for (var i = 0; i < qty; i++)
            {
                var input = Convert.ToInt32(Console.ReadLine());
                testCases[i] = input;
            }

            for (var i = 0; i < qty; i++)
            {
                GetBestPlace(testCases[i]);
            }
            Console.Read();
        }

        private static void GetBestPlace(int testCase)
        {
            var initArray = Enumerable.Range(1, testCase).ToArray();
            Console.WriteLine(DrawArray(initArray));

            while (initArray.Length > 1)
            {
                var newArrSize = initArray.Length/2;
                var newArr = new int[newArrSize];
                for (var i = 0; i < newArrSize; i++)
                {
                    newArr[i] = initArray[i*2 + 1];
                }
                initArray = newArr;
                Console.WriteLine(DrawArray(initArray));
            }
        }

        private static string DrawArray(int[] arr)
        {
            var sb = new System.Text.StringBuilder();
            foreach (var t in arr)
            {
                sb.Append(t + " ");
            }
            return sb.ToString();
        }
    }
}

csharp 序列化程序c#

序列化程序c#

serializer.cs
public static T XmlDeserializeFromString<T>(string objectData)
{
    return (T)XmlDeserializeFromString(objectData, typeof(T));
}

public static object XmlDeserializeFromString(string objectData, Type type)
{
    var serializer = new XmlSerializer(type);
    object result;

    using (TextReader reader = new StringReader(objectData))
    {
        result = serializer.Deserialize(reader);
    }

    return result;
}

// To Clean XML
public static string SerializeToString<T>(T value)
{
    var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
    var serializer = new XmlSerializer(value.GetType());
    var settings = new XmlWriterSettings {
        Indent = true, 
        OmitXmlDeclaration = true
    };

    using (var stream = new StringWriter())
    using (var writer = XmlWriter.Create(stream, settings))
    {
        serializer.Serialize(writer, value, emptyNamepsaces);
        return stream.ToString();
    }
}

public static string XmlSerializeToString(object objectInstance)
{
    var emptyNs = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
    var serializer = new XmlSerializer(objectInstance.GetType());
    var sb = new StringBuilder();

    using (TextWriter writer = new StringWriter(sb))
    {
        serializer.Serialize(writer, objectInstance, emptyNs);
    }

    return sb.ToString();
}

csharp 列出所有子文件夹中的所有文件。

列出所有子文件夹中的所有文件。

listFiles.cs
 public static void ListFiles(FileSystemInfo info)
        {
            if (!info.Exists) return;

            DirectoryInfo dir = info as DirectoryInfo;
            
            if (dir == null) return;

            FileSystemInfo[] files = dir.GetFileSystemInfos();

            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i] as FileInfo;

                if (file != null && file.Extension == ".edmx")
                {
                    if (!file.IsReadOnly)
                    {
                        edmxFileList.Add(file);
                    }
                }
                else
                    ListFiles(files[i]);
            }
        }