如何使用反射来映射列表
如果我有一个对象
public class Car {
public List<UpdatePart> updatePartList {get; set;}
}
public class UpdatePart {
public PartToModify PartToModify {get; set;}
public string NewValue {get;set}
}
并且 PartToModify 是一个枚举:
public enum PartToModify {
Engine,
Tire,
}
我有一个 Part 对象:
public class Part {
public string Engine {get;set;}
public string Tire {get;set;}
public decimal price {get;set;}
}
如何使用反射以及对于与 PartToModify 中的枚举匹配的 Part 上的每个属性,创建一个新的 UpdatePart 对象并选择正确的枚举,其中 PartToModify = = Part.Property 并将 Part.Property 的值分配给 UpdatePart.NewValue。
我会这样说:
var partProperties = partObj.GetType().GetProperties();
foreach (var property on updatePartProperties) {
UpdatePartList.Add(MapProperties(partProperties, part));
}
public UpdatePart MapProperties(PropertyInfo partProperties, Part partObj){
//pseudo code
var updatePart = new UpdatePart();
foreach(var property on partProperties) {
if (property.Name == <iterate through enum values until one is found>)
updatePart.PartToModify = PartToModify.<somehow select value that matches property.name>
updatePart.NewValue = property.GetValue(partObj, null);
}
return updatePart;
}
显然你明白我想做的事情,有什么想法吗?不,这不是一个学校项目。整个“汽车”示例是我想出的最接近实际对象的最快示例,因为我不想只写出我想要完成的任务,而是想提供一个示例。
If I have an object
public class Car {
public List<UpdatePart> updatePartList {get; set;}
}
public class UpdatePart {
public PartToModify PartToModify {get; set;}
public string NewValue {get;set}
}
And the PartToModify is an enum:
public enum PartToModify {
Engine,
Tire,
}
And I have a Part object:
public class Part {
public string Engine {get;set;}
public string Tire {get;set;}
public decimal price {get;set;}
}
How can I use reflection and for every property on Part that matches a enum in PartToModify, create a new UpdatePart object and select the correct enum where PartToModify == Part.Property and assigns the UpdatePart.NewValue the value of Part.Property.
I would something like:
var partProperties = partObj.GetType().GetProperties();
foreach (var property on updatePartProperties) {
UpdatePartList.Add(MapProperties(partProperties, part));
}
public UpdatePart MapProperties(PropertyInfo partProperties, Part partObj){
//pseudo code
var updatePart = new UpdatePart();
foreach(var property on partProperties) {
if (property.Name == <iterate through enum values until one is found>)
updatePart.PartToModify = PartToModify.<somehow select value that matches property.name>
updatePart.NewValue = property.GetValue(partObj, null);
}
return updatePart;
}
Obviously you get the jiff of what I am trying to do, any thoughts? And no, this is not a school project. The whole "car" example was the quickest closest example I came up with to the actual objects, since I didn't want to just write out what I was trying to accomplish, wanted to provide an example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不完全确定您在这里要做什么,但要通过字符串值查找枚举的值,您可以使用
Enum.Parse
或Enum.TryParse
。您可以执行以下操作:
第二个
true
参数告诉它忽略大小写。所以你可以匹配所有的“Engine”,“engine”,“ENGINE”等。你也可以反过来,通过使用来查找枚举中的所有名称
I'm not completely sure what you are trying to do here but to find a value of an enum by it's string value you could use
Enum.Parse
orEnum.TryParse
.You could do something like this:
The 2nd
true
parameter tells it to ignore case. so you could match all of "Engine", "engine", "ENGINE", etc..You could also go the other way around and just find all names that are in the enum by using