首先创建一个EntityMapper类
public class EntityMapper
{
public static T ConvertEntity < T, V> ( V v) where T : class
where V : class
{
try
{
var t_type = typeof ( T ) ;
var t_entity = Activator. CreateInstance < T> ( ) ;
var v_type = typeof ( V ) ;
var property_t = t_type. GetProperties ( System. Reflection. BindingFlags. Instance | System. Reflection. BindingFlags. Public) ;
var property_v = v_type. GetProperties ( System. Reflection. BindingFlags. Instance | System. Reflection. BindingFlags. Public) ;
foreach ( var _v in property_v)
{
var v_propertyName = _v. Name;
var value = _v. GetValue ( v) ;
foreach ( var t in property_t)
{
var t_propertyName = t. Name;
if ( t. IsDefined ( typeof ( CustomPropertyAttribute ) , true ) )
{
var t_attribute = t. GetCustomAttributes ( typeof ( CustomPropertyAttribute ) , true ) ;
foreach ( CustomPropertyAttribute item in t_attribute)
{
var v_name = property_v. FirstOrDefault ( p => p. Name == item. Name) ;
if ( v_name is not null )
{
t. SetValue ( t_entity, v_name. GetValue ( v) ) ;
}
}
}
else
{
if ( v_propertyName. ToLower ( ) == t_propertyName. ToLower ( ) )
{
if ( t_entity != null )
{
t. SetValue ( t_entity, value ) ;
break ;
}
}
}
}
}
return t_entity;
}
catch ( Exception ex)
{
throw new IndexOutOfRangeException ( ex. Message) ;
}
}
public static List< T> ConvertEntity < T, V> ( List< V> v_list) where T : class where V : class
{
try
{
var t_list = new List< T> ( ) ;
if ( v_list. Count == 0 || v_list is null )
{
return t_list;
}
var v_type = typeof ( V ) ;
var v_property = v_type. GetProperties ( ) ;
var t_type = typeof ( T ) ;
var t_property = t_type. GetProperties ( ) ;
foreach ( var v_item in v_list)
{
var t_entity = Activator. CreateInstance < T> ( ) ;
foreach ( var t_p in t_property)
{
if ( t_p. IsDefined ( typeof ( CustomPropertyAttribute ) , true ) )
{
var t_attribute = t_p. GetCustomAttributes ( typeof ( CustomPropertyAttribute ) , true ) ;
foreach ( CustomPropertyAttribute item in t_attribute)
{
var v_name = typeof ( V ) . GetProperties ( ) . FirstOrDefault ( p => p. Name. ToLower ( ) == item. Name. ToLower ( ) ) ;
if ( v_name is not null )
{
t_p. SetValue ( t_entity, v_name. GetValue ( v_item) ) ;
}
}
}
else
{
var temp = v_property. FirstOrDefault ( p => p. Name == t_p. Name) ;
if ( temp is not null )
{
t_p. SetValue ( t_entity, temp. GetValue ( v_item) ) ;
}
}
}
t_list. Add ( t_entity) ;
}
return t_list;
}
catch ( Exception ex)
{
throw new IndexOutOfRangeException ( ex. Message) ;
}
}
}
创建一个自定义特性CustomPropertyAttribute
[ AttributeUsage ( AttributeTargets. Property, AllowMultiple = false ) ]
public class CustomPropertyAttribute : Attribute
{
public CustomPropertyAttribute ( string name)
{
Name = name;
}
public string Name { get ; set ; }
}
创建一个Student类
public class Student
{
public int Id { get ; set ; }
public string Name { get ; set ; }
}
创建一个Teacher类
public class Teacher
{
[ CustomProperty ( "Id" ) ]
public int sd { get ; set ; }
public string Name { get ; set ; }
}
调用
var stus = new List< Student> ( ) { new Student { Id = 1 , Name = "锤锤" } , new Student { Id = 2 , Name = "王大锤" } } ;
var stu = new Student { Id= 1 , Name = "123" } ;
var a = EntityMapper. ConvertEntity < Teacher, Student> ( stus) ;
var b = EntityMapper. ConvertEntity < Teacher, Student> ( stu) ;
Console. ReadLine ( ) ;