Copy with Object Model to Dynamic Object

0 thích 0 không thích
1 lượt xem
đã hỏi 13 Tháng 5, 2022 trong Lập trình C# bởi nguyenthao (9,000 điểm)
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var person = new Person() { Id = 1, Name = "Lập Trình VB.NET" };       

            dynamic eo = CopyWithOjbectModel(person);
            var b = new List<Person>();
            b.Add(new Person() { Id = 4, Name = "a" });
            b.Add(new Person() { Id = 5, Name = "b" });
            b.Add(new Person() { Id = 6, Name = "c" });
            eo.Status = 1;
            eo.Name = "Nguyễn Thảo";
            eo.TrangThai = "Đẹp trai";
            eo.Id = 2;
            eo.Result = b;

            var data = JsonConvert.SerializeObject(eo);
            Console.ReadLine();
        }

        public static dynamic CopyWithOjbectModel<T>(T modelCopyWith)
        {
            string json = JsonConvert.SerializeObject(modelCopyWith);
            dynamic dynamicObject = JsonConvert.DeserializeObject<ExpandoObject>(json);
            return dynamicObject;
        }

        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
            public string Telephone { get; set; }
        }

      
    }
}

Looking for an answer?  Share this question:     

Xin vui lòng đăng nhập hoặc đăng ký để trả lời câu hỏi này.

...