C# 扩展方法帮助 - 将集合转换为另一种类型
我想转换自
public class Party
{
public string Type { get; set; }
public string Name { get; set; }
public string Status { get; set;}
}
并转换为
public class Contact
{
public string Type { get; set; }
public string Name { get; set; }
public string Status { get; set;}
public string Company {get;set;}
public string Source {get;set;}
}
我尝试使用此扩展方法
public static class EnumerableExtensions
{
public static IEnumerable<TTo> ConvertTo<TTo, TFrom>(this IEnumerable<TFrom> fromList)
{
return ConvertTo<TTo, TFrom>(fromList, TypeDescriptor.GetConverter(typeof(TFrom)));
}
public static IEnumerable<TTo> ConvertTo<TTo, TFrom>(this IEnumerable<TFrom> fromList, TypeConverter converter)
{
return fromList.Select(t => (TTo)converter.ConvertTo(t, typeof(TTo)));
}
}
我收到此错误 TypeConverter 无法将“派对”转换为“联系人”
var parties = new List<Party>();
parties.Add(new Party { Name = "name 1", Status = "status 1" });
parties.Add(new Party { Name = "name 2", Status = "status 2" });
var results = parties.ConvertTo<Contact, Party>().ToList();
我在这里缺少什么?
I want to convert from
public class Party
{
public string Type { get; set; }
public string Name { get; set; }
public string Status { get; set;}
}
and convert to
public class Contact
{
public string Type { get; set; }
public string Name { get; set; }
public string Status { get; set;}
public string Company {get;set;}
public string Source {get;set;}
}
I tried using this extension method
public static class EnumerableExtensions
{
public static IEnumerable<TTo> ConvertTo<TTo, TFrom>(this IEnumerable<TFrom> fromList)
{
return ConvertTo<TTo, TFrom>(fromList, TypeDescriptor.GetConverter(typeof(TFrom)));
}
public static IEnumerable<TTo> ConvertTo<TTo, TFrom>(this IEnumerable<TFrom> fromList, TypeConverter converter)
{
return fromList.Select(t => (TTo)converter.ConvertTo(t, typeof(TTo)));
}
}
I get this error TypeConverter is unable to convert 'Party' to 'Contact'
var parties = new List<Party>();
parties.Add(new Party { Name = "name 1", Status = "status 1" });
parties.Add(new Party { Name = "name 2", Status = "status 2" });
var results = parties.ConvertTo<Contact, Party>().ToList();
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类型 Party 和类型接触之间没有直接转换(多态或基于继承)。
您需要为您的类型实现 TypeConverter,以便 .NET 知道如何在这两种类型之间进行转换:
MSDN - 类型转换器类
MSDN -如何实现 TypeConverter
创建 TypeConverter 后,您必须使用 TypeConverterAttribute 装饰两个类,以便框架可以在运行时获取 TypeConverter 的实例:
您还可以尝试模仿(我的内容)我猜测)是使用 LINQ 所需的行为(即使您将失去通用能力):
There is no direct conversion between the type Party and the Type contact (polymorphic or inheritance based).
You need to implement a TypeConverter for your types so that .NET knows how to convert between those two types:
MSDN - TypeConverter Class
MSDN - How to Implement a TypeConverter
Once you create your TypeConverters, you have to decorate your two classes with the TypeConverterAttribute so that the Framework can get an instance of your TypeConverter at Runtime:
You could also attempt to mimic the (what I'm guessing) is the desired behavior using LINQ (even though you'll lose the generic abilities):
这是一种略有不同的方法,尽管其核心仍然是简单地将成员值从一个对象复制到另一个对象,
然后将其添加到您的 Contact 类中
,然后像这样进行转换:
您可能希望将最后一位包含在某些扩展方法中,但我不这样做没看出一点……
here is a slightly different way, though at its core it still about simply copying member values from one object to another
add this to your Contact class
then convert like this:
you may want to enclose last bit into some extension method, but I don't see a point...