两个不相同的类之间如果赋值

Posted dandan00056

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了两个不相同的类之间如果赋值相关的知识,希望对你有一定的参考价值。

    /// <summary>         
    /// 类属性/字段的值复制工具        
    /// </summary>       
    public class ClassValueCopier
    
        /// <summary>           
        ///  复制           
        ///  </summary>             
        ///  <param name="destination">目标</param>             
        ///  <param name="source">来源</param>             
        ///  <returns>成功复制的值个数</returns>            
        public static int Copy(object destination, object source)
        
            if (destination == null || source == null)
            
                return 0;
            
            return Copy(destination, source, source.GetType());
        


        /// <summary>            
        /// 复制          
        /// </summary>            
        /// <param name="destination">目标</param>          
        /// <param name="source">来源</param>           
        /// <param name="type">复制的属性字段模板</param>          
        /// <returns>成功复制的值个数</returns>           
        public static int Copy(object destination, object source, Type type)
        
            return Copy(destination, source, type, null);
        
        /// <summary>           
        /// 复制          
        /// </summary>      
        /// <param name="destination">目标</param>        
        /// <param name="source">来源</param>           
        /// <param name="type">复制的属性字段模板</param>           
        /// <param name="excludeName">排除下列名称的属性不要复制</param>             
        /// <returns>成功复制的值个数</returns>            
        public static int Copy(object destination, object source, Type type, IEnumerable<string> excludeName)
        
            if (destination == null || source == null)
             return 0; 
            if (excludeName == null)
            
                excludeName = new List<string>();
            
            int i = 0;
            Type desType = destination.GetType();
            foreach (FieldInfo mi in type.GetFields())
            
                if (excludeName.Contains(mi.Name))
                 continue; 
                try
                
                    FieldInfo des = desType.GetField(mi.Name);
                    if (des != null && des.FieldType == mi.FieldType)
                    
                        des.SetValue(destination, mi.GetValue(source)); i++;
                    
                
                catch
                
                
            
            foreach (PropertyInfo pi in type.GetProperties())
            
                if (excludeName.Contains(pi.Name))
                
                    continue;
                
                try
                
                    PropertyInfo des = desType.GetProperty(pi.Name);
                    if (des != null && des.PropertyType == pi.PropertyType && des.CanWrite && pi.CanRead)
                    
                        des.SetValue(destination, pi.GetValue(source, null), null);
                        i++;
                    
                
                catch
                
                    //throw ex;                  
                
            
            return i;
        
    

    #region 扩展方法 For .NET 3.0+
    /// <summary>        
    /// 类属性/字段的值复制工具 扩展方法     
    /// </summary>      
    public static class ClassValueCopier2
    
        /// <summary>             
        /// 获取实体类的属性名称           
        /// </summary>            
        /// <param name="source">实体类</param>         
        /// <returns>属性名称列表</returns>            
        public static List<string> GetPropertyNames(this object source)
        
            if (source == null)
            
                return new List<string>();
            
            return GetPropertyNames(source.GetType());
        
        /// <summary>            
        /// 获取类类型的属性名称(按声明顺序)           
        /// </summary>            
        /// <param name="source">类类型</param>             
        /// <returns>属性名称列表</returns>           
        public static List<string> GetPropertyNames(this Type source)
        
            return GetPropertyNames(source, true);
        
        /// <summary>           
        /// 获取类类型的属性名称        
        /// </summary>           
        /// <param name="source">类类型</param>           
        /// <param name="declarationOrder">是否按声明顺序排序</param>       
        /// <returns>属性名称列表</returns>            
        public static List<string> GetPropertyNames(this Type source, bool declarationOrder)
        
            if (source == null)
            
                return new List<string>();
            
            var list = source.GetProperties().AsQueryable();
            if (declarationOrder)
            
                list = list.OrderBy(p => p.MetadataToken);
            
            return list.Select(o => o.Name).ToList(); ;
        
        /// <summary>         
        /// 从源对象赋值到当前对象    
        /// </summary>         
        /// <param name="destination">当前对象</param>      
        /// <param name="source">源对象</param>        
        /// <returns>成功复制的值个数</returns>         
        public static int CopyValueFrom(this object destination, object source)
        
            return CopyValueFrom(destination, source, null);
        
        /// <summary>           
        /// 从源对象赋值到当前对象           
        /// </summary>           
        /// <param name="destination">当前对象</param>           
        /// <param name="source">源对象</param>           
        /// <param name="excludeName">排除下列名称的属性不要复制</param>       
        /// <returns>成功复制的值个数</returns>             
        public static int CopyValueFrom(this object destination, object source, IEnumerable<string> excludeName)
        
            if (destination == null || source == null)
            
                return 0;
            
            return ClassValueCopier.Copy(destination, source, source.GetType(), excludeName);
        
        /// <summary>     
        /// 从当前对象赋值到目标对象        
        /// </summary>          
        /// <param name="source">当前对象</param>      
        /// <param name="destination">目标对象</param>           
        /// <returns>成功复制的值个数</returns>            
        public static int CopyValueTo(this object source, object destination)
        
            return CopyValueTo(destination, source, null);
        
        /// <summary>           
        /// 从当前对象赋值到目标对象          
        /// </summary>           
        /// <param name="source">当前对象</param>         
        /// <param name="destination">目标对象</param>        
        /// <param name="excludeName">排除下列名称的属性不要复制</param>      
        /// <returns>成功复制的值个数</returns>        
        public static int CopyValueTo(this object source, object destination, IEnumerable<string> excludeName)
        
            if (destination == null || source == null)
            
                return 0;
            
            return ClassValueCopier.Copy(destination, source, source.GetType(), excludeName);
        
    
    #endregion

  

以上是关于两个不相同的类之间如果赋值的主要内容,如果未能解决你的问题,请参考以下文章

两个具有相同属性的类赋值

C#用反射实现两个类的对象之间相同属性的值的复制

C#将一个类中的数值赋值给另一个类 两个相同类型的类,例如 Class c1; Clas

C语言中两个相同类型的结构体变量之间是可以相互直接赋值的

在C语言中.结构体变量之间可以相互赋值吗

在 C# 中的类之间共享对象