映射两个相同类型的对象(不包括某些字段)的最佳方式是什么?我之前在这里发布了我的问题,但我没有得到任何回应,因为-我猜-它太笼统了。我会尝试更简洁。我有两个相同类型的对象,我想映射一些属性并排除其他属性。我想要做的是将一个对象保存在缓存中,稍后使用具有特定属性的属性(字段)获取它。我看过Automapper,但没有找到适合我的东西,所以我正在考虑实施我自己的系统。我创建了一个属性:[AttributeUsage(AttributeTargets.Property,AllowMultiple=false,Inherited=false)]publicclassFilterFieldAttribute:Attribute{}并在我需要包含的字段上装饰了一个类:publicclassOrdersViewModel:BaseViewModel{...[FilterField][DisplayName("OrderNumber:")]publicstringOrderNumber{get;放;}[FilterField][DisplayName("Fromdate:")]publicDateTimeFromDate{get;放;}[FilterField][DisplayName("Todate:")]publicDateTimeToDate{get;放;}[DisplayName("Status:")]publicintStatus{get;放;}...}现在,我实现了一个负责映射的函数:privateTMap(TSource,TDestination)whereT:BaseViewModel{if(Source==null){return(Source);}FilterFieldAttributefilterAttribute;foreach(PropertyInfopropInfointypeof(T).GetProperties()){foreach(FilterFieldAttributeattrinpropInfo.GetCustomAttributes(typeof(FilterFieldAttribute),false)){if(filterAttribute!=null){varvalue=propInfo.GetValue(Source,null);propInfo.SetValue(目的地,价值,空);}}}返回(目的地);现在,当我需要从缓存中获取我的视图模型并仅填充标有我的代码的属性时,我的代码如下所示:viewModel=Map(myCache.Get(Key)asT,viewModel);我不知道这是否是最好的方法,但这似乎是我发现的唯一方法任何建议将不胜感激。使用直接反射(如示例中所示)会很慢;给出更完整的答案是棘手的,因为这取决于您是想要子对象的浅克隆还是深层克隆。无论哪种方式,您都应该期望这涉及一些元编程和缓存-使用ILGenerator或Expression。但是,对于惰性选项,序列化可能很有用。许多序列化程序允许您使用属性来包含/排除特定项目;通过序列化到内存流,倒带(.Position=0)和反序列化,您应该获得所选成员的深层副本。以下是使用表达式的示例:usingSystem;使用System.ComponentModel;使用System.Linq;使用System.Linq.Expressions;[AttributeUsage(AttributeTargets.Property,AllowMultiple=false,Inherited=false)]publicclassFilterFieldAttribute:Attribute{publicstaticTClone(Tobj)其中T:class,new(){returnCache.clone(obj);}privatestaticclassCachewhereT:class,new(){publicstaticreadonlyFuncclone;staticCache(){varparam=Expression.Parameter(typeof(T),"source");varmembers=frompropintypeof(T).GetProperties()whereAttribute.IsDefined(prop,typeof(FilterFieldAttribute))选择Expression.Bind(prop,Expression.Property(param,prop));varnewObj=Expression.MemberInit(Expression.New(typeof(T)),members);clone=Expression.Lambda>(newObj,param).Compile();}}}publicclassOrdersViewModel{[FilterField][DisplayName("OrderNumber:")]publicstringOrderNumber{get;放;}[FilterField][DisplayName("开始日期:")]publicDateTimeFromDate{get;放;}[FilterField][DisplayName("Todate:")]publicDateTimeToDate{get;放;}[DisplayName("Status:")]publicintStatus{get;放;}staticvoidMain(){varfoo=newOrdersViewModel{OrderNumber="abc",FromDate=DateTime.Now,ToDate=DateTime.Now,Status=1};varbar=FilterFieldAttribute.Clone(foo);听起来是个不错的开始,但是您正在失去AutoMapper的许多好处AutoMapper还可以处理此处没有的嵌套属性(当您的类包含嵌套的映射类时)。由于AutoMapper是一个oprn源项目,我建议您使用AutoMapper源并在那里实现过滤。事实上,它也可能对其他人有益。我认为你的方法没问题。反射有一些性能影响——值得考虑。另一种有效且简单的方法可能是让BaseViewModel定义一个抽象方法:publicabstractBaseViewModelToCacheVersion();可用于将子类转换为正确的类型。每个子类都会处理自己的映射:以上是C#学习教程:映射两个相同类型的对象(不包括某些字段)的最佳方式是什么?如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——publicclassViewModelX{publicViewModelX(stringname,stringdescription){Name=name;描述=描述;}。..publicoverrideBaseViewModelToCacheVersion(){returnnewViewModelX(Name,//包括名称。null//忽略描述。);}...}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
