public override object GetParameterValue(ParameterType parameter)
        {
            var reflectionData = CommandReflection.GetReflectionData(this.GetType());
            var propInfo       = reflectionData.MappedProperties[parameter];
            var type           = propInfo.PropertyType;

            if (type.Equals(typeof(bool)))
            {
                return(null);
            }
            else if (type.IsEnum)
            {
                return((decimal)(int)propInfo.GetValue(this, null));
            }
            else if (type.IsGenericType && type.GetGenericArguments()[0].IsEnum)
            {
                var val = propInfo.GetValue(this, null);
                if (val == null)
                {
                    return(null);
                }
                return((decimal)(int)val);
            }
            else
            {
                var val = propInfo.GetValue(this, null);
                if (val == null)
                {
                    return(null);
                }
                var dec = (decimal)Convert.ChangeType(val, typeof(decimal));
                return(dec);
            }
        }
        public static CommandMapping FromTokens(string[] tokens)
        {
            var commandLetter = (CommandType)Enum.Parse(typeof(CommandType), tokens[0]);
            int commandNumber = int.Parse(tokens[1]);
            var type          = CommandReflection.GetCommandObjectType(commandLetter, commandNumber);

            if (type == null)
            {
                throw new Exception("No mapping defined for these tokens");
            }
            return(FromTokens(type, tokens));
        }
Exemplo n.º 3
0
 public static CommandBase FromTokens(string[] tokens, bool useMappedCommands = true)
 {
     if (useMappedCommands)
     {
         var commandLetter = (CommandType)Enum.Parse(typeof(CommandType), tokens[0]);
         int commandNumber = int.Parse(tokens[1]);
         var type          = CommandReflection.GetCommandObjectType(commandLetter, commandNumber);
         if (type != null)
         {
             return(CommandMapping.FromTokens(tokens));
         }
     }
     return(Command.FromTokens(tokens));
 }
        public override IEnumerable <ParameterType> GetParameters()
        {
            var reflectionData = CommandReflection.GetReflectionData(this.GetType());

            foreach (var kv in reflectionData.MappedProperties)
            {
                var paramType = kv.Key;
                var propInfo  = kv.Value;

                if (propInfo.PropertyType.Equals(typeof(bool)) && (bool)propInfo.GetValue(this, null) == true)
                {
                    yield return(paramType);
                }
                else if (GetParameterValue(paramType) != null)
                {
                    yield return(paramType);
                }
            }
        }
        public override void SetParameterValue(ParameterType parameter, object value)
        {
            var reflectionData = CommandReflection.GetReflectionData(this.GetType());
            var propInfo       = reflectionData.MappedProperties[parameter];

            var  type           = propInfo.PropertyType;
            bool isNullableType = type.Equals(typeof(string)) || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>));

            if (type.Equals(typeof(bool)) || type.Equals(typeof(bool?)))
            {
                propInfo.SetValue(this, true, null);
            }
            else if (value == null)
            {
                if (isNullableType)
                {
                    propInfo.SetValue(this, null, null);
                }
                else if (type.IsEnum)
                {
                    propInfo.SetValue(this, 0, null);
                }
                else
                {
                    propInfo.SetValue(this, Convert.ChangeType(0, type), null);
                }
            }
            else if (value != null)
            {
                if (type.Equals(typeof(string)))
                {
                    propInfo.SetValue(this, value, null);
                }
                else if (type.Equals(typeof(byte)) || type.Equals(typeof(byte?)))
                {
                    propInfo.SetValue(this, Convert.ChangeType(value, typeof(byte)), null);
                }
                else if (type.Equals(typeof(int)) || type.Equals(typeof(int?)))
                {
                    propInfo.SetValue(this, Convert.ChangeType(value, typeof(int)), null);
                }
                else if (propInfo.PropertyType.Equals(typeof(double)) || propInfo.PropertyType.Equals(typeof(double?)))
                {
                    propInfo.SetValue(this, Convert.ChangeType(value, typeof(double)), null);
                }
                else if (propInfo.PropertyType.Equals(typeof(decimal)) || propInfo.PropertyType.Equals(typeof(decimal?)))
                {
                    propInfo.SetValue(this, Convert.ChangeType(value, typeof(decimal)), null);
                }
                else if (type.IsEnum)
                {
                    var enumVal = Enum.ToObject(type, Convert.ChangeType(value, typeof(int)));
                    propInfo.SetValue(this, enumVal, null);
                }
                else if (type.IsGenericType && type.GetGenericArguments()[0].IsEnum)
                {
                    var enumVal = Enum.ToObject(type.GetGenericArguments()[0], Convert.ChangeType(value, typeof(int)));
                    propInfo.SetValue(this, enumVal, null);
                }
                else
                {
                    throw new Exception("Command property type not supported: " + type);
                }
            }
        }
        public override bool HasParameter(ParameterType parameter)
        {
            var reflectionData = CommandReflection.GetReflectionData(this.GetType());

            return(reflectionData.MappedProperties.ContainsKey(parameter));
        }