C# 映射帮助类, Mapper

C# 映射帮助类, Mapper

代码开源,在下面这个链接:

https://gitee.com/daolizhe/cuyan.orm

环境

.net framework 2.0 +

核心代码:

public static class Mapper
{

    /// <summary>
    /// 映射,无需初始化(方便,但效率低)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="T1">The type of the 1.</typeparam>
    /// <param name="t">The t.</param>
    /// <returns></returns>
    public static T1 Map<T, T1>(T t)
    {
        var source = Activator.CreateInstance(typeof(T));
        var result = Activator.CreateInstance(typeof(T1));
        if (source.GetType().Name == "List`1" || result.GetType().Name == "List`1")
        {
            throw new Exception("形参有误!,请使用对象。");
            //throw new Exception("列表映射请使用 MapList 方法");
        }
        var tpropertyInfos = source.GetType().GetProperties();
        var t1propertyInfos = result.GetType().GetProperties();
        foreach (var tinfo in tpropertyInfos)
        {
            foreach (var t1info in t1propertyInfos)
            {
                if (t1info.PropertyType.IsValueType || t1info.PropertyType.Name.StartsWith("String"))
                {
                    if (tinfo.Name == t1info.Name)
                    {
                        try
                        {
                            object value = tinfo.GetValue(t, null);
                            var property = typeof(T1).GetProperty(tinfo.Name);
                            if (property != null && property.CanWrite && !(value is DBNull))
                            {
                                property.SetValue(result, value, null);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }

        }
        return (T1)result;
    }

    /// <summary>
    /// 映射,无需初始化(方便,但效率低)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="T1">The type of the 1.</typeparam>
    /// <param name="t">The t.</param>
    /// <returns></returns>
    public static List<T1> MapList<T, T1>(List<T> t)
    {
        List<T1> result = new List<T1>();
        if (t == null)
        {
            throw new Exception("未将对象引用设置到对象的实例。");
        }
        foreach (var item in t)
        {
            result.Add(Map<T, T1>(item));
        }
        return result;
    }
}

完整代码

调用代码:

//获取到一个List集合
var students = _db.Query<Student>("SELECT * FROM [Student]", null);
//将 Student 集合映射到 StudentModel 集合里
var studentModels = Mapper.MapList<Student, StudentModel>(students);

Mapper代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace cuyan.orm
{
    public static class Mapper
    {

        /// <summary>
        /// 映射,无需初始化(方便,但效率低)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T1">The type of the 1.</typeparam>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        public static T1 Map<T, T1>(T t)
        {
            var source = Activator.CreateInstance(typeof(T));
            var result = Activator.CreateInstance(typeof(T1));
            if (source.GetType().Name == "List`1" || result.GetType().Name == "List`1")
            {
                throw new Exception("形参有误!,请使用对象。");
                //throw new Exception("列表映射请使用 MapList 方法");
            }
            var tpropertyInfos = source.GetType().GetProperties();
            var t1propertyInfos = result.GetType().GetProperties();
            foreach (var tinfo in tpropertyInfos)
            {
                foreach (var t1info in t1propertyInfos)
                {
                    if (t1info.PropertyType.IsValueType || t1info.PropertyType.Name.StartsWith("String"))
                    {
                        if (tinfo.Name == t1info.Name)
                        {
                            try
                            {
                                object value = tinfo.GetValue(t, null);
                                var property = typeof(T1).GetProperty(tinfo.Name);
                                if (property != null && property.CanWrite && !(value is DBNull))
                                {
                                    property.SetValue(result, value, null);
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                }

            }
            return (T1)result;
        }

        /// <summary>
        /// 映射,无需初始化(方便,但效率低)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T1">The type of the 1.</typeparam>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        public static List<T1> MapList<T, T1>(List<T> t)
        {
            List<T1> result = new List<T1>();
            if (t == null)
            {
                throw new Exception("未将对象引用设置到对象的实例。");
            }
            foreach (var item in t)
            {
                result.Add(Map<T, T1>(item));
            }
            return result;
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容