如何使用字典映射类型和操作来自动转换值?

时间:2020-07-18 12:53:46

标签: c# dictionary

我希望系统可以使用字典根据输入值的类型自动转换值。

但是下面的代码将显示错误消息。

无法将对象转换为空

我该如何进行这项工作?

    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;
        }
    }

2 个答案:

答案 0 :(得分:1)

您将通过将Action更改为Func 并添加字符串的类型映射来工作。

    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;
        }
    }

答案 1 :(得分: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;
            }
        }