【问题标题】:C# help with Extension Method - Converting collection to another type扩展方法的 C# 帮助 - 将集合转换为另一种类型
【发布时间】:2011-03-29 15:53:24
【问题描述】:

我想转换

 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 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();

我在这里错过了什么?

【问题讨论】:

    标签: c# generics extension-methods type-conversion


    【解决方案1】:

    这是一种稍微不同的方式,尽管它的核心仍然是简单地将成员值从一个对象复制到另一个对象

    将此添加到您的联系人类

    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)
    

    您可能想将最后一点包含在一些扩展方法中,但我没有看到任何意义...

    【讨论】:

      【解决方案2】:

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

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

      MSDN - TypeConverter Class

      MSDN - How to Implement a 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
      });
      

      【讨论】:

      • 是的,LINQ 方法会起作用。认为可能有另一种方法。
      • 很遗憾没有。您要么必须实现 TypeConverter 方法,要么使用 LINQ。
      猜你喜欢
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2018-06-25
      相关资源
      最近更新 更多