C#

根据参考对象对已有对象进行格式化

一个基本的对象,一个缺少值的对象,将缺少值的对象的属性设置为基本对象属性的值

Posted by Trojan on June 11, 2019

根据参考对象对已有对象进行格式化

首先定义一个类

public class AddressInfo
{
    public int Id { get; set; }
    public string userName { get; set; }
    public string userTel { get; set; }
}

new 两个对象,一个为基础的用来参考的对象,一个为有属性未设置值的对象。

// 其他对象如果值为默认值的,都用这个对象的值进行替代
var baseInfo = new AddressInfo() {
    Id=1,
    userName="Trojan",
    userTel="****"
};
// 这是一个新的对象,它的 UserName 为null
var info =new AddressInfo()
{
    Id = 2,
    userTel = "1234567890"
};

我们要把这个对象的空值属性的值设置为我们参考对象的的属性的值。 一般我们的做法是:

if (info.Id==0)
{
    info.Id = baseInfo.Id;
}
if (info.userName==null)
{
    info.userName = baseInfo.userName;
}
if (info.userTel==null)
{
    info.userTel = baseInfo.userTel;
}

如果有对象有几十个属性的话。那不是要**996**了?


所以:

新建一个公共方法,采用反射对其进行赋值

public static T ObjectToMerge<T>(T reference, T obj)
{
    // 循环对象的每一个属性
    foreach (var item in obj.GetType().GetProperties())
    {
        // 获取这个属性的值
        var value = item.GetValue(obj);
        // 获取这个属性的默认值
        var defaultValue = GetDefaultForType(item.PropertyType);
        // 判断这个属性入默认值是否相同
        if (object.Equals(value, defaultValue))
        {
            // 如果是默认值的话采用 我们提供的参考值进行赋值
            item.SetValue(obj, item.GetValue(reference));
        }
    }
    return obj;
}
public static object GetDefaultForType(Type type)
{
    return type.IsValueType ? Activator.CreateInstance(type) : null;
}

然后:

info = ObjectToMerge(baseInfo, info);

就好了

未调用方法之前:

20190611152323.png
调用方法后:

20190611152404.png

完整代码在这里:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;

namespace ConsoleApp1
{

    class Program
    {
        static void Main(string[] args)
        {
            // 基础的参考对象
            // 其他对象如果值为默认值的,都用这个对象的值进行替代
            var baseInfo = new AddressInfo()
            {
                Id = 1,
                userName = "Trojan",
                userTel = "****"
            };
            // 这是一个新的对象,它的 UserName 为null
            var info = new AddressInfo()
            {
                Id = 2,
                userTel = "1234567890"
            };

            // 新建一个公共方法,采用反射对其进行赋值
            info = ObjectToMerge(baseInfo, info);
        }

        public static T ObjectToMerge<T>(T reference, T obj)
        {
            // 循环对象的每一个属性
            foreach (var item in obj.GetType().GetProperties())
            {
                // 获取这个属性的值
                var value = item.GetValue(obj);
                // 获取这个属性的默认值
                var defaultValue = GetDefaultForType(item.PropertyType);
                // 判断这个属性入默认值是否相同
                if (object.Equals(value, defaultValue))
                {
                    // 如果是默认值的话采用 我们提供的参考值进行赋值
                    item.SetValue(obj, item.GetValue(reference));
                }
            }
            return obj;
        }
        public static object GetDefaultForType(Type type)
        {
            return type.IsValueType ? Activator.CreateInstance(type) : null;
        }
    }
    public class AddressInfo
    {
        public int Id { get; set; }
        public string userName { get; set; }
        public string userTel { get; set; }
    }
}