使用反射和extesnion方法对象之间复制特性 [英] Copy properties between objects using reflection and extesnion method
问题描述
这是我的代码,我创建一个对象(实体)的拷贝到自定义对象。结果
拷贝它只是源和目标在同一名称的属性。
我的问题是,当一个实体有navgiaton到另一个实体,这种情况下,我加入了自定义属性我想补充的自定义类属性上面。
例如自定义类的样子:
公共类CourseModel :BaseDataItemModel
{
公众诠释CourseNumber {搞定;组; }
公共字符串名称{;组; }
LecturerModel讲师;
[PropertySubEntity]
公共LecturerModel讲师
{
{返回讲师; }
集合{讲师=价值; }
}
公共CourseModel()
{
讲师=新LecturerModel();
}
}
问题是在 targetProp.CopyPropertiesFrom(sourceProp);
行,当我再次尝试调用扩展方法(复制嵌套的对象),因为该类型是在运行时确定的,扩展方法不能得到解决在编译的时候。
也许我失去了一些东西...
公共静态无效CopyPropertiesFrom(这BaseDataItemModel targetObject,对象源)
{
的PropertyInfo [] = allProporties source.GetType()的GetProperties()。
的PropertyInfo targetProperty;
的foreach(的PropertyInfo fromProp在allProporties)
{
targetProperty = targetObject.GetType()的getProperty(fromProp.Name)。
如果(targetProperty == NULL)继续;
如果(targetProperty.CanWrite!)继续; //检查
如果在目标类属性标记SkipProperty属性
如果(targetProperty.GetCustomAttributes(typeof运算(SkipPropertyAttribute),TRUE)。长度!= 0)继续;
如果(targetProperty.GetCustomAttributes(typeof运算(PropertySubEntity),TRUE)。长度!= 0)
{
//类型PTYPE = targetProperty.PropertyType;
VAR targetProp = targetProperty.GetValue(targetObject,NULL);
VAR sourceProp = fromProp.GetValue(源,NULL);
targetProp.CopyPropertiesFrom(sourceProp); //< ==问题就在这里
//targetProperty.SetValue(targetObject,sourceEntity,NULL);
}
,否则
targetProperty.SetValue(targetObject,fromProp.GetValue(源,NULL),NULL);
}
}
您'会得先投
((BaseDataItemModel)targetProp).CopyPropertiesFrom(sourceProp);
This is my code where I create a "copy" of one object (Entity) into a custom object.
It copies just properties with the same name in both source and target.
My problem is when an Entity has a navgiaton to another Entity, for this case I added a custom attribute that I add above the property in the custom class.
For example the custom class looks like:
public class CourseModel:BaseDataItemModel
{
public int CourseNumber { get; set; }
public string Name { get; set; }
LecturerModel lecturer;
[PropertySubEntity]
public LecturerModel Lecturer
{
get { return lecturer; }
set { lecturer = value; }
}
public CourseModel()
{
lecturer = new LecturerModel();
}
}
The problem is in targetProp.CopyPropertiesFrom(sourceProp);
line, when I try to call extension method again (to copy the nested object) ,because the type is determined on run time, extension method couldn't resolved on compile time.
Maybe I am missing something...
public static void CopyPropertiesFrom(this BaseDataItemModel targetObject, object source)
{
PropertyInfo[] allProporties = source.GetType().GetProperties();
PropertyInfo targetProperty;
foreach (PropertyInfo fromProp in allProporties)
{
targetProperty = targetObject.GetType().GetProperty(fromProp.Name);
if (targetProperty == null) continue;
if (!targetProperty.CanWrite) continue;
//check if property in target class marked with SkipProperty Attribute
if (targetProperty.GetCustomAttributes(typeof(SkipPropertyAttribute), true).Length != 0) continue;
if (targetProperty.GetCustomAttributes(typeof(PropertySubEntity), true).Length != 0)
{
//Type pType = targetProperty.PropertyType;
var targetProp = targetProperty.GetValue(targetObject, null);
var sourceProp = fromProp.GetValue(source, null);
targetProp.CopyPropertiesFrom(sourceProp); // <== PROBLEM HERE
//targetProperty.SetValue(targetObject, sourceEntity, null);
}
else
targetProperty.SetValue(targetObject, fromProp.GetValue(source, null), null);
}
}
You'll have to cast first.
((BaseDataItemModel)targetProp).CopyPropertiesFrom(sourceProp);
这篇关于使用反射和extesnion方法对象之间复制特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!