分享

数据转换

 昵称70680357 2020-07-02

实体对象数据转结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//原始对象
Test test = new Test();
test.Name = "反射";
var props = test.GetType().GetProperties();
StructData structData = new StructData();
Object someBoxedStruct = structData;
StructData someUnBoxedStruct = new StructData();
foreach (var item in props)
{
    var setobj = structData.GetType().GetProperty(item.Name);
    //从旧对象中获取值
    var value = item.GetValue(test, null);
    Type myType = typeof(StructData);
    FieldInfo myFieldInfo = myType.GetField(item.Name);
    myFieldInfo.SetValue(someBoxedStruct, value.ToString());
    someUnBoxedStruct = (StructData)someBoxedStruct;
}

  

实体对象A转实体对象B

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//原始数据
Test test = new Test();
var tests = test.GetType().GetProperties();
//赋值对象
Test1 test1 = new Test1();
foreach (var inst in tests)
{
语言 方法
8006 wn2Z3
C0FaM
  • 互联网创业项目之抖音修复照片日进万元「纯干货」
  • 9260 2005.05.12 16-39-59
        var test1Name = test1.GetType().GetProperty(inst.Name);
        if (test1Name==null)
        {
            continue;
        }
        //获取数据
        var value = inst.GetValue(test, null);
        test1Name.SetValue(test1, value);
    }

      本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
      转藏 分享 献花(0

      0条评论

      发表

      请遵守用户 评论公约

      类似文章 更多