本文介绍了复制作为 IDbSet&lt;T> 的属性.到另一个类的属性,它是 List&lt;T>使用反射具有相同的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我们在单元测试中有一个场景,我们创建了一个实现 IDbSet 的 FakeDbSet.在 FakeUnitOfwork 中,我的属性是 IDbSets 并使用 FakeDbSet 进行更新.We have a scenario in our unit tests where we have created a FakeDbSet that implements IDbSet. In a FakeUnitOfwork I have properties that are IDbSets and getting new-ed up using the FakeDbSet.无需在我的 Commit 方法中写出每个不同的属性,我尝试使用反射来迭代 FakeUnitOfWork 中的属性.然后,我想将属性值复制到具有相同类型的 List 属性的不同类.所以我的 FakeUnitOfWork 中可能有一个属性:Without having to write out each different property in my Commit method I am trying to use reflection to iterate over the properties inside the FakeUnitOfWork. I then want to copy the property values to a differnt class that has propertes of List<> of the same type. So I may have a property in my FakeUnitOfWork:IDbSet<User> Users {get {return _users ?? (_users = new FakeDbSet<User>());}在我的假数据存储中,我有这个属性:In my fake data store I have this property:List<User> Users {get;set;}这是我到目前为止所拥有的:This is hwat I have so far:public void Commit(){ foreach (var property in typeof(TestUnitOfWork).GetProperties()) { var testContextType = typeof (TestDataContext).GetProperty(property.Name);//I then want to do a ToList() on the TestUnitOfWork IDbSet properties to push them into the TestDataContext. }}所以,我不确定如何知道我正在查看一个 IDbSet(来自 FakeUnitOfWork)和一个 List(来自我的假内存数据存储),以便我可以将数据从 FakeUnitOfWork 复制到数据存储.由于它们具有相同的名称,因此我只需要弄清楚如何通过反射进行转换.So, I am not sure how to know I am looking at, say, a IDbSet (from FakeUnitOfWork) and a List (from my fake memory data store) so that I can copy the data from FakeUnitOfWork over to the data store. Since they have the same names I only need to figure out how to do the casting via reflection.更新:我尝试过这样的事情,并认为我可能在正确的轨道上,但代码永远不会被击中:Update: I tried something like this and thought I might be on the irght track, but the code never gets hit:foreach (var property in typeof(TestUnitOfWork).GetProperties()) { var testContextType = typeof (TestDataContext).GetProperty(property.Name); if(property.GetValue(this,null) is IDbSet<MyBaseEntityType>) { testContextType.SetValue(TestDataContext, ((IDbSet<MyBaseEntityType>) property.GetValue(this,null)).ToList(),null); } }推荐答案您使用反射来获取上下文和工作单元的类型和属性.现在,您在一侧拥有 List 类型的属性,并且您希望将 IDbSet 类型的属性的内容分配给它.对吗?You used a reflection to get types and properties for your context and unit of work. Now you have property of type List<> on one side and you want to assign content of property of type IDbSet<> to it. Is it right?理论为此,您需要在 IDbSet 上调用 ToList 方法,但此方法不是 IDbSet 接口的一部分.它是 System.Linq.Enumerable 静态类中定义的扩展方法.扩展方法只是带有语法糖的静态方法,但它仍然可以作为普通的静态方法调用.因此,您必须使用反射(获取类型)查找 Enumerable 类,获取 ToList 方法的通用方法信息并将其定位到适当的通用参数(由当前 IDbSet).然后你可以将你的集合作为参数传递给这个方法并调用它.To do that you need to call ToList method on IDbSet<> but this method is not part of IDbSet<> interface. It is extension method defined in System.Linq.Enumerable static class. Extension method is just static method with syntactic sugar but it can still be called as normal static method. So you must lookup Enumerable class using reflection (get type), get generic method info for ToList method and target it to proper generic argument (used by current IDbSet<>). Then you can pass your set as parameter to this method and invoke it.无论如何更好的方法是避免尽可能多的反射.例如,在 TestUnitOfWork 上公开特殊接口,提供对列表的直接访问.Anyway better approach would be avoiding as many reflection as possible. For example expose special interface on TestUnitOfWork which would offer direct access to lists. 这篇关于复制作为 IDbSet&lt;T> 的属性.到另一个类的属性,它是 List&lt;T>使用反射具有相同的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-30 03:11