C# 扩展方法帮助 - 将集合转换为另一种类型

发布于 2024-09-14 10:25:26 字数 1411 浏览 7 评论 0原文

我想转换自

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装纯掩盖桑 2024-09-21 10:25:26

类型 Party 和类型接触之间没有直接转换(多态或基于继承)。

您需要为您的类型实现 TypeConverter,以便 .NET 知道如何在这两种类型之间进行转换:

MSDN - 类型转换器类

MSDN -如何实现 TypeConverter

创建 TypeConverter 后,您必须使用 TypeConverterAttribute 装饰两个类,以便框架可以在运行时获取 TypeConverter 的实例:

public class PartyTypeConverter : TypeConverter
{
    // Implementation
}

[TypeConverter(typeof(PartyTypeConverter)]
public class Party
{
    public string Type { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
}

您还可以尝试模仿(我的内容)我猜测)是使用 LINQ 所需的行为(即使您将失去通用能力):

var contacts = parties.Select(p => new Contact {
    Type = p.Type,
    Name = p.Name,
    Status = p.Status
});

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:

public class PartyTypeConverter : TypeConverter
{
    // Implementation
}

[TypeConverter(typeof(PartyTypeConverter)]
public class Party
{
    public string Type { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
}

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):

var contacts = parties.Select(p => new Contact {
    Type = p.Type,
    Name = p.Name,
    Status = p.Status
});
萌酱 2024-09-21 10:25:26

这是一种略有不同的方法,尽管其核心仍然是简单地将成员值从一个对象复制到另一个对象,

然后将其添加到您的 Contact 类中

public static explicit operator Contact(Party p)
{
    return new Contact
    {
        Type = p.Type,
        Name = p.Name,
        Status = p.Status
    };
}

,然后像这样进行转换:

List<Party> parties = new List<Party>();
parties.Add(new Party { Name = "Foo", Status = "Bar" });

parties.Select<Party, Contact>(p => (Contact)p)

您可能希望将最后一位包含在某些扩展方法中,但我不这样做没看出一点……

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

public static explicit operator Contact(Party p)
{
    return new Contact
    {
        Type = p.Type,
        Name = p.Name,
        Status = p.Status
    };
}

then convert like this:

List<Party> parties = new List<Party>();
parties.Add(new Party { Name = "Foo", Status = "Bar" });

parties.Select<Party, Contact>(p => (Contact)p)

you may want to enclose last bit into some extension method, but I don't see a point...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文