C# 反射操作方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Dscf.Global.Utils
{
    public class ReflectionHelper
    {
        /// <summary>
        /// 创建对象实例
        /// </summary>
        /// <typeparam name="T">要创建对象的类型</typeparam>
        /// <param name="assemblyName">类型所在程序集名称</param>
        /// <param name="nameSpace">类型所在命名空间</param>
        /// <param name="className">类型名</param>
        /// <returns></returns>
        public static T CreateInstance<T>(string assemblyName,string nameSpace, string className)
        {
            try
            {
                string fullName = nameSpace + "." + className;//命名空间.类型名
                //此为第一种写法
                Assembly assembly = null;
                try
                {
                    assembly = Assembly.Load(assemblyName);
                }
                catch(Exception ex)
                {
                    string s = assemblyName.ToLower();
                    s = s.Replace(".dll","");
                    assembly = Assembly.Load(s);
                }
                object ect = assembly.CreateInstance(fullName);//加载程序集,创建程序集里面的 命名空间.类型名 实例
                return (T)ect;//类型转换并返回
                //下面是第二种写法
                //string path = fullName + "," + assemblyName;//命名空间.类型名,程序集
                //Type o = Type.GetType(path);//加载类型
                //object obj = Activator.CreateInstance(o, true);//根据类型创建实例
                //return (T)obj;//类型转换并返回
            }
            catch(Exception e)
            {
                //发生异常,返回类型的默认值
                return default(T);
            }
        }
        /// <summary>
        /// 比较两个类中属性发生改变,并返回改变的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetChangeStrInfo<T>(T told,T tnew)
        {
            if (told == null || tnew == null)
            {
                throw new Exception(" told or tnew is null");
            }
            Type type = typeof(T);
            PropertyInfo[] props = type.GetProperties();
            StringBuilder builder = new StringBuilder();
            string s="";
            foreach (PropertyInfo p in props)
            {
                
                object oldv = p.GetValue(told, null);
                object newv = p.GetValue(tnew, null);
                
                if (oldv != null || newv != null)
                {
                    s = p.Name + "[";
                    string s1 = oldv != null ? oldv.ToString() : "";
                    string s2 = newv != null ? newv.ToString() : "";
                    if (s1 != s2)
                    {
                        s += (s1 + "->" + s2+"]");
                        if (builder.Length > 0) builder.Append(",");
                        builder.Append(s);
                    }
                }
            }
            return builder.ToString();
        }
        /// <summary>
        /// 两个对像赋值,只要属性名一样,就赋值,这个用于报表那里
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        public static void PropertySetValue(object source, object target)
        {
            //PropertyInfo[] props = source.GetType().GetProperties();
            PropertyInfo[] drProps = target.GetType().GetProperties();
            Type sourceType = source.GetType();
            foreach (PropertyInfo p in drProps)
            {
                PropertyInfo sourceProp = sourceType.GetProperty(p.Name);
                if (sourceProp != null)
                {
                    if (sourceProp.GetValue(source,null)!=null)
                    p.SetValue(target, Convert.ChangeType(sourceProp.GetValue(source, null), p.PropertyType), null);
                }
            }
        }
    
    }
}
原文地址:https://www.cnblogs.com/dullbaby/p/4838041.html