1

I want the system can auto convert value depends on type of input value by using dictionary.

But the code below will show the error message.

cannot convert object into void

How can I make this work?

    public class testClass
    {
        public int? TESTNULLABLEINT { get; set; }
        public int TESTINT { get; set; }
        public string TESTSTRING { get; set; }
        public float TESTFLOAT { get; set; }
        public MailAddress TESTMAIL { get; set; }
        public bool TESTBOOL { get; set; }
    }
    static void Main(string[] args)
    {
        var TypeMapping = new Dictionary<Type, Action<string>>();
        TypeMapping.Add(typeof(int), (x) => Convert.ToInt32(x));
        TypeMapping.Add(typeof(int?), (x) => Convert.ToInt32(x));
        TypeMapping.Add(typeof(float), (x) => Convert.ToSingle(x));
        TypeMapping.Add(typeof(float?), (x) => Convert.ToSingle(x));
        TypeMapping.Add(typeof(MailAddress), (x) => new MailAddress(x));
        TypeMapping.Add(typeof(bool), (x) =>  Convert.ToBoolean(Convert.ToInt32(x)));
    
        List<string> inputValue = new List<string>();
        
        inputValue.Add(null);
        inputValue.Add("100");
        inputValue.Add("test");
        inputValue.Add("0.2");
        inputValue.Add("test@test.com");
        inputValue.Add("1");
    
        testClass test = new testClass();
        int cnt = 0;
        foreach (PropertyInfo item in test.GetType().GetProperties())
        {
            //"cannot convert object into void"
            item.SetValue(test, TypeMapping[item.PropertyType](inputValue[cnt]));
            cnt = cnt + 1;
        }
    }
qjuanp
  • 2,736
  • 1
  • 16
  • 18
EvaHHHH
  • 293
  • 3
  • 16
  • 1
    `Action` doesn't return a value. `Func` does. [For example](https://stackoverflow.com/a/59441301/7444103) – Jimi Jul 18 '20 at 13:10

2 Answers2

1

You're code will work by changing Action to Func<string, object> and adding a type mapping for string.

    public class testClass
    {
        public int? TESTNULLABLEINT { get; set; }
        public int TESTINT { get; set; }
        public string TESTSTRING { get; set; }
        public float TESTFLOAT { get; set; }
        public MailAddress TESTMAIL { get; set; }
        public bool TESTBOOL { get; set; }
    }

    static void Main(string[] args)
    {
        var TypeMapping = new Dictionary<Type, Func<string, object>>();
        TypeMapping.Add(typeof(int), x => Convert.ToInt32(x));
        TypeMapping.Add(typeof(int?), x => Convert.ToInt32(x));
        TypeMapping.Add(typeof(float), x => Convert.ToSingle(x));
        TypeMapping.Add(typeof(float?), x => Convert.ToSingle(x));
        TypeMapping.Add(typeof(MailAddress), x => new MailAddress(x));
        TypeMapping.Add(typeof(bool), x => Convert.ToBoolean(Convert.ToInt32(x)));
        TypeMapping.Add(typeof(string), x => x);

        List<string> inputValue = new List<string>();

        inputValue.Add(null);
        inputValue.Add("100");
        inputValue.Add("test");
        inputValue.Add("0.2");
        inputValue.Add("test@test.com");
        inputValue.Add("1");

        testClass test = new testClass();
        int cnt = 0;
        foreach (PropertyInfo item in test.GetType().GetProperties())
        {
            item.SetValue(test, TypeMapping[item.PropertyType](inputValue[cnt]));
            cnt = cnt + 1;
        }
    }
sspaniel
  • 647
  • 3
  • 10
0

public class testClass
    {
        public int? TESTNULLABLEINT { get; set; }
        public int TESTINT { get; set; }
        public string TESTSTRING { get; set; }
        public float TESTFLOAT { get; set; }
        public string TESTMAIL { get; set; }
        public bool TESTBOOL { get; set; }
    }

 static void convertprop()
        {
            var TypeMapping = new Dictionary<int, Type>();
            TypeMapping.Add(0, typeof(int));
            TypeMapping.Add(1, typeof(int));
            TypeMapping.Add(2, typeof(string));
            TypeMapping.Add(3, typeof(float));
          
             TypeMapping.Add(4, typeof(string));
            TypeMapping.Add(5, typeof(bool));

            List<string> inputValue = new List<string>();
         
            inputValue.Add("0");
            inputValue.Add("100");
            inputValue.Add("test");
            inputValue.Add("0.2");
            inputValue.Add("test@test.com");
            inputValue.Add("true");
            var invalues = inputValue.ToArray();
            testClass test = new testClass();
            int cnt = 0;
            foreach (System.Reflection.PropertyInfo item in test.GetType().GetProperties())
            {
                //"cannot convert object into void"
                Type value;
                TypeMapping.TryGetValue(cnt, out value);
               
                    item.SetValue(test, Convert.ChangeType(inputValue[cnt], value));
              
               
                cnt = cnt + 1;
            }
        }

 static void convertprop()
        {
            var TypeMapping = new Dictionary<int, Type>();
            TypeMapping.Add(0, typeof(int));
            TypeMapping.Add(1, typeof(int?));
            TypeMapping.Add(2, typeof(float));
            TypeMapping.Add(3, typeof(float?));
             TypeMapping.Add(4, typeof(MailAddress));
            TypeMapping.Add(5, typeof(bool));

            List<string> inputValue = new List<string>();

            inputValue.Add(null);
            inputValue.Add("100");
            inputValue.Add("test");
            inputValue.Add("0.2");
            inputValue.Add("test@test.com");
            inputValue.Add("1");

            testClass test = new testClass();
            int cnt = 0;
            foreach (System.Reflection.PropertyInfo item in test.GetType().GetProperties())
            {
                //"cannot convert object into void"
                Type value;
                TypeMapping.TryGetValue(cnt, out value);
                item.SetValue(test, Convert.ChangeType(inputValue[cnt],value));
                cnt = cnt + 1;
            }
        }
LDS
  • 354
  • 3
  • 9
  • The list is string type values ,So when comapre to ref type ,it throw errors.So please convert the class TESTMAIL to string type.nullable int is not accepted Thanks – LDS Jul 19 '20 at 04:30