Switch(Type) en C#

       private bool SwitchWithDataType(Type dataType, object value)
        {
            bool result = false;

            Dictionary<Type, Action> @switch = new Dictionary<Type, Action>
            {
                {
                    typeof(short), () => { result = TryParseToShort(value); }
                },
                {
                    typeof(int), () =>{ result= TryParseToInt(value); }
                },
                {
                    typeof(long), () =>{ result= TryParseToLong(value); }
                },
                {
                    typeof(string), ()=>{ result=  TryParseToString(value); }
                }
            };

            @switch[dataType]();

            return result;
        }

 

Agregar comentario

Loading