金蝶云星空——DynamicObject对象

1.说明

  • DynamicObject是K3Cloud的基础数据格式

  • DynamicObject相当于一个有层次结构的数据字典。
    有层次怎么理解?其实就DynamicObject里面有很多层,是通过键值对的方式构成的,而每个值是有不同的数据类型的。
    当然也会存在DynamicObject对象类型的值,所以就形成了DynamicObject多层展开的数据结构。
    字典的key对应的就是字段的实体属性标识

  • DynamicObjectCollection里面有多个或者1个DynamicObject。是DynamicObject集合,相当于是DynamicObject的数组。

2.代码示例

三种类型:

  • DynamicObject(动态数据包),
  • DynamicObjectType(动态实体类型),
  • DynamicProperty(动态实体类型)

        using Kingdee.BOS.Orm.DataEntity;
        using Kingdee.BOS.Orm.Metadata.DataEntity;
        using Newtonsoft.Json;
        using System;


        public static void TestDynamicObject()
        {
            #region 创建一个DynamicObjectType studentType

            //创建名称为"studentType"的动态实体类型
            DynamicObjectType studentType = new DynamicObjectType("Student");

            //注册简单属性:Id,整型,默认值:0
            DynamicProperty IdType = studentType.RegisterSimpleProperty("Id", typeof(int), 1);

            //注册简单属性:Name,字符串类型
            DynamicProperty nameType = studentType.RegisterSimpleProperty("Name", typeof(string));

            //注册复杂属性:住址
            DynamicObjectType addressType = new DynamicObjectType("Address");
            addressType.RegisterSimpleProperty("Province", typeof(string));
            addressType.RegisterSimpleProperty("City", typeof(string));
            studentType.RegisterComplexProperty("Address", addressType, false);

            //注册集合属性:爱好集合
            DynamicObjectType hobbyType = new DynamicObjectType("Hobby");
            hobbyType.RegisterSimpleProperty("HobbyName", typeof(string), "", false);
            hobbyType.RegisterSimpleProperty("HobbyId", typeof(int), 0, false);
            DynamicObjectCollection hobbyColl = new DynamicObjectCollection(hobbyType);
            studentType.RegisterCollectionProperty("Hobbies", hobbyType, hobbyColl.GetType());




            //打印序列化后的类型:studentType
            Console.WriteLine(JsonConvert.SerializeObject(studentType));

            #endregion

            #region 实例化一个自定义的studentType类型的DynamicObject

            //创建实例方式1:
            object studentLiSi = studentType.CreateInstance();
            Console.WriteLine(JsonConvert.SerializeObject(studentLiSi));
            DynamicObject student1 = studentLiSi as DynamicObject;


            //创建实例方式2:
            DynamicObject stuZhangSan = new DynamicObject(studentType);
            stuZhangSan["Name"] = "张三";
            stuZhangSan["Id"] = 1;

            DynamicObject addressZhangSan = new DynamicObject(addressType);
            addressZhangSan["Province"] = "江苏省";
            addressZhangSan["City"] = "苏州市";
            stuZhangSan["Address"] = addressZhangSan;

            DynamicObject hobby1 = new DynamicObject(hobbyType);
            hobby1["HobbyName"] = "篮球";
            hobby1["HobbyId"] = 100;
            DynamicObject hobby2 = new DynamicObject(hobbyType);
            hobby2["HobbyName"] = "足球";
            hobby2["HobbyId"] = 101;
            DynamicObjectCollection hobbyCol = new DynamicObjectCollection(hobbyType) { hobby1, hobby2 };


            //注意:DynamicObject中的DynamicObjectCollection的属性是只读,不能赋值
            //stuZhangSan["Hobbies"]= hobbyCol;//这里报错:实体属性Hobbies被标记为只读。
            DynamicObjectCollection docHobbies = stuZhangSan["Hobbies"] as DynamicObjectCollection;
            docHobbies.Add(hobby1);
            docHobbies.Add(hobby2);


            Console.WriteLine(JsonConvert.SerializeObject(stuZhangSan));

            #endregion

            #region 复制一个DynamicObject

            //基于某个DynamicObject对象创建一个新的相同类型的对象,并对新对象添加属性:Gender
            DynamicObjectType stuZhangSanType = stuZhangSan.DynamicObjectType;
            stuZhangSanType.RegisterSimpleProperty("Gender",typeof(string),"1");

            DynamicObject newStu = new DynamicObject(stuZhangSan.DynamicObjectType);
            foreach (DynamicProperty property in stuZhangSan.DynamicObjectType.Properties)
            {
                if (property.Name == "Hobbies")//这里因为hobbies属性是只读的,所以需要单独处理
                {
                    DynamicObjectCollection oldHobbies = stuZhangSan["Hobbies"] as DynamicObjectCollection;
                    DynamicObjectCollection newHobbies = newStu["Hobbies"] as DynamicObjectCollection;
                    foreach (DynamicObject item in oldHobbies)
                    {
                        newHobbies.Add(item);
                    }
                }
                else
                {
                    newStu[property.Name] = stuZhangSan[property.Name];
                }

            }
            Console.WriteLine(JsonConvert.SerializeObject(newStu));

            #endregion

            #region 读取DynamicObject属性

            //方式1:直接读取
            string stuId = stuZhangSan["Id"].ToString();
            string stuName = stuZhangSan["Name"].ToString();
            DynamicObject stuAddress = stuZhangSan["Address"] as DynamicObject;
            string province = stuAddress["Province"].ToString();
            string city = stuAddress["City"].ToString();
            DynamicObjectCollection stuHobbies = stuZhangSan["Hobbies"] as DynamicObjectCollection;
            string stuHobby1 = stuHobbies[0]["HobbyName"].ToString();
            string stuHobby2 = stuHobbies[1]["HobbyName"].ToString();
            

            Console.WriteLine($"Id:{stuId},Name:{stuName},province:{province} ,city:{city},stuHobby1:{stuHobby1},stuHobby2:{stuHobby2}");
            
            #endregion

        }
  • 打印:studentType序列化结果:
{
  "Name": "Student",
  "ExtendName": "Student",
  "Properties": [
    {
      "Name": "Id",
      "PropertyType": "System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
      "DefaultValue": 1,
      "HasDefaultValue": true,
      "Attributes": [
        
      ],
      "IsReadonly": false,
      "Hashcode": null
    },
    {
      "Name": "Name",
      "PropertyType": "System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
      "DefaultValue": null,
      "HasDefaultValue": false,
      "Attributes": [
        
      ],
      "IsReadonly": false,
      "Hashcode": null
    },
    {
      "ComplexPropertyType": {
        "Name": "Address",
        "ExtendName": "Address",
        "HashCode": -1911254792,
        "Properties": [
          {
            "Name": "Province",
            "PropertyType": "System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
            "DefaultValue": null,
            "HasDefaultValue": false,
            "Attributes": [
              
            ],
            "IsReadonly": false,
            "Hashcode": null
          },
          {
            "Name": "City",
            "PropertyType": "System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
            "DefaultValue": null,
            "HasDefaultValue": false,
            "Attributes": [
              
            ],
            "IsReadonly": false,
            "Hashcode": null
          }
        ]
      },
      "Name": "Address",
      "PropertyType": "Kingdee.BOS.Orm.DataEntity.DynamicObject, Kingdee.BOS.DataEntity, Version=8.2.886.8, Culture=neutral, PublicKeyToken=null",
      "DefaultValue": null,
      "HasDefaultValue": false,
      "Attributes": [
        
      ],
      "IsReadonly": false,
      "Hashcode": null
    },
    {
      "ItemPropertyType": {
        "Name": "Hobby",
        "ExtendName": "Hobby",
        "HashCode": -1907744442,
        "Properties": [
          {
            "Name": "HobbyName",
            "PropertyType": "System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
            "DefaultValue": "",
            "HasDefaultValue": true,
            "Attributes": [
              
            ],
            "IsReadonly": false,
            "Hashcode": null
          },
          {
            "Name": "HobbyId",
            "PropertyType": "System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
            "DefaultValue": 0,
            "HasDefaultValue": false,
            "Attributes": [
              
            ],
            "IsReadonly": false,
            "Hashcode": null
          }
        ]
      },
      "Name": "Hobbies",
      "PropertyType": "Kingdee.BOS.Orm.DataEntity.DynamicObjectCollection, Kingdee.BOS.DataEntity, Version=8.2.886.8, Culture=neutral, PublicKeyToken=null",
      "DefaultValue": null,
      "HasDefaultValue": false,
      "Attributes": [
        
      ],
      "IsReadonly": true,
      "Hashcode": null
    }
  ]
}
  • 打印:stuZhangSan序列化结果
{
  "Id": 1,
  "Name": "张三",
  "Address": {
    "Province": "江苏省",
    "City": "苏州市"
  },
  "Hobbies": [
    {
      "HobbyName": "篮球",
      "HobbyId": 100
    },
    {
      "HobbyName": "足球",
      "HobbyId": 101
    }
  ]
}
  • 打印:newStu对象序列化
{
  "Id": 1,
  "Name": "张三",
  "Address": {
    "Province": "江苏省",
    "City": "苏州市"
  },
  "Hobbies": [
    {
      "HobbyName": "篮球",
      "HobbyId": 100
    },
    {
      "HobbyName": "足球",
      "HobbyId": 101
    }
  ],
  "Gender": "1"
}
  • 打印:读取DynamicObject属性
Id:1,Name:张三,province:江苏省 ,city:苏州市,stuHobby1:篮球,stuHobby2:足球

3.参考

posted @ 2024-12-10 08:10  shanzm  阅读(15)  评论(0编辑  收藏  举报
TOP