首页新闻找找看学习计划

通过C#反射把一个对象的值给另一个对象报错"对象类型与目标对象类型不一致"

0
[待解决问题]
public class Test
   {
       public double Menuid { get; set; }
       public double Userid { get; set; }
       public string Menuname { get; set; }
       public string Name { get; set; }
       public DateTime Operatetime { get; set; }
       public string Operateip { get; set; }
       public string Operatetype { get; set; }
   }
public class Entity
   {
       public double Menuid { get; set; }
       public double Userid { get; set; }
       public string Menuname { get; set; }
       public string Name { get; set; }
       public DateTime Operatetime { get; set; }
       public string Operateip { get; set; }
       public string Operatetype { get; set; }
   }
测试:
Entity entity = new Entity
{
    Menuid = 1.0f,
    Menuname = "Case",
    Name = "mike",
    Operateip = "192.168.10.113",
    Operatetime = DateTime.Now.ToLocalTime(),
    Operatetype = "Edit",
    Userid = 102.0f
};
try
{
    Test t = new Test();
 
 
    var en = entity.GetType().GetProperties();
    foreach (var prop in en)
    {
        var value = prop.GetValue(entity, null);
        if (prop != null && prop.CanWrite)
        {
            prop.SetValue(t, Convert.ChangeType(value, prop.PropertyType), null);
            //if (!prop.PropertyType.IsGenericTypeDefinition)
            //{
            //    prop.SetValue(t, (string.IsNullOrEmpty(value.ToString()) ? null : Convert.ChangeType(value, prop.PropertyType)), null);
            //}
            //else
            //{
            //    Type genericTypeDefinition = prop.PropertyType.GetGenericTypeDefinition();
            //    if (genericTypeDefinition == typeof(Nullable<>))
            //    {
            //        prop.SetValue(t, (string.IsNullOrEmpty(value.ToString()) ? null : Convert.ChangeType(value,Nullable.GetUnderlyingType( prop.PropertyType))), null);
            //    }
            //}
 
        }
    }
    Console.WriteLine(t.Name);
}
catch (Exception ex)
{
    ex.ToString();
}
#endregion
问题补充:

假如传过来的对象有10个属性与后台处理的对象的5个属性?请问这个是否能用反射解决?谢谢!

jackcnblog的主页 jackcnblog | 菜鸟二级 | 园豆:206
提问于:2015-12-03 13:27
< >
分享
所有回答(3)
0

你调试的时候看看是哪个属性出错,值是啥。

爱编程的大叔 | 园豆:30271 (高人七级) | 2015-12-03 14:22

刚进去的时候,value的值是1.0,然后就报错是了? 

支持(0) 反对(0) jackcnblog | 园豆:206 (菜鸟二级) | 2015-12-03 19:28

@jackcnblog: 别自己写了,AutoMapper,能搞定你所有的这些需求,包括你补充的需求。

支持(0) 反对(0) 爱编程的大叔 | 园豆:30271 (高人七级) | 2015-12-04 10:25
0

问题出在你定义了两个类Test和Entity,虽说这两个类有相同的属性,但是指向的确是不同的堆.

你要Entity的属性反射到Test里,如Test.Menuid和Entity.Menuid当然不是同一块指向,所以就报出这样的错误

Test t = new Test();改成Entity t = new Entity();就可以反射成功,当然这也没什么意义.

如果你想把Entity的值赋值给Test最好还是手动赋值一下.

澈澈 | 园豆:435 (菜鸟二级) | 2015-12-03 14:34
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            Entity entity = new Entity
            {
                Menuid = 1.0f,
                Menuname = "Case",
                Name = "mike",
                Operateip = "192.168.10.113",
                Operatetime = DateTime.Now.ToLocalTime(),
                Operatetype = "Edit",
                Userid = 102.0f
            };
            try
            {
                Test t = new Test();


                var en = entity.GetType().GetProperties();
                var enT = t.GetType().GetProperties();
                foreach (System.Reflection.PropertyInfo prop in en)
                {
                    var value = prop.GetValue(entity, null);
                    if (prop != null && prop.CanWrite)
                    {

                        foreach (System.Reflection.PropertyInfo item in enT)
                        {
                            if (prop.Name.Equals(item.Name))
                            {
                                item.SetValue(t, value);
                            }
                        }
                    }

                }
                Console.WriteLine(t.Name);
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
    }
    public class Test
    {
        public double Menuid { get; set; }
        public double Userid { get; set; }
        public string Menuname { get; set; }
        public string Name { get; set; }
        public DateTime Operatetime { get; set; }
        public string Operateip { get; set; }
        public string Operatetype { get; set; }
    }

    public class Entity
    {
        public double Menuid { get; set; }
        public double Userid { get; set; }
        public string Menuname { get; set; }
        public string Name { get; set; }
        public DateTime Operatetime { get; set; }
        public string Operateip { get; set; }
        public string Operatetype { get; set; }
    }
}
田麦成 | 园豆:1982 (小虾三级) | 2015-12-03 14:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册