c# - 由于泛型类型推断的限制,无法使用泛型来改进 API

标签 c# .net generics type-inference

我有一个类旨在收集任何类型的对象并创建它的导出(例如 Excel 电子表格)。我可以根据自己的选择提供列名和宽度:

这是类(class)的总结:

public class ObjectExporter
{
    public string Export<T>(IEnumerable<T> objects, IEnumerable<ColumnDefinition> columnDefinitions)
    {
      //Iterate through list of ColumnDefinitions.
      //Output value of the property in each object...
      //...whose name is equal to the PropertyName of the current ColumnDefinition
    }
}

这是 ColumnDefinition 类:

public class ColumnDefinition  {
    public string ColumnName { get; set; }
    public string PropertyName { get; set; }
    public int Width { get; set; }
    public ColumnDefinition(string columnName, string propertyName, int width)
    {
      ColumnName = columnName;
      PropertyName = propertyName;
      Width = width;
    }
  }

这是一个用法示例:

private void TestObjectExporter()
{
      ObjectExporter objectExporter = new ObjectExporter();
      //Note that the following variable changeRequests is a collection of anonymous type
      var changeRequests = ChangeRequestRepository.All.Take(5).Select(x => new { x.ChangeRequested, x.DateCreated, x.CreatedBy.Username });
      Response.Write(objectExporter.Export(changeRequests, new List<ColumnDefinition>()
                                              {
                                                new ColumnDefinition("Change Requested", "ChangeRequested", 12),
                                                new ColumnDefinition("Date Created", "DateCreated", 12),
                                                new ColumnDefinition("Created By", "Username", 12)
                                              }));
}

重要的是 ObjectExporter 可以处理匿名类型的集合。我想修改 ObjectExporter 和 ColumnDefinition 类以使用如下所示的强类型语法(注意属性名称的指定方式):

Response.Write(objectExporter.Export(changeRequests, new List<ColumnDefinition>()
                                              {
                                                new ColumnDefinition("Change Requested", x => x.ChangeRequested, 12),
                                                new ColumnDefinition("Date Created", x => x.DateCreated, 12),
                                                new ColumnDefinition("Created By", x => x.Username, 12)
                                              }));

我相信这样做的方法是创建一个 ColumnDefinition<T>类(class)。但是,我找不到让编译器推断出 T 的方法。用于 IEnumerable<ColumnDefinition<T>>参数与 IEnumerable<T> 中使用的相同范围。这意味着我不能再将类与匿名类型的集合一起使用,因为我不能显式指定泛型类型参数。

谁能想出办法做到这一点?

最佳答案

在你的列定义类中,你会做这样的事情:

public class ColumnDefinition<T>
{
     public ColumnDefinition(string displayName, Expression<Func<T, string>> propertyExpression, int width)
     {
     }
}

您的对象导出器签名也会更改:

public string Export<T>(IEnumerable<T> items, IEnumerable<ColumnDefinition<T>> columns)
{
}

然后,T 的类型应该从传递的可枚举中推断出来,因此第二个参数需要一个 IEnumerable>,这意味着您的 ColumnDefinition 类推断类型 T,然后构造函数中的表达式选择 T。

编辑:或者:

将您的 Enumerable 包装到另一个类中,然后调用它的方法。

public class ObjectWrapper<T> : IDisposable
{
     public IEnumerable<T> Items { get; protected set; }
     public IEnumerable<ColumnDefinition> Definitions { get; set; }     

     public ObjectWrapper(IEnumerable<T> source) 
     { 
         Items = source; 
         Definitions = new List<ColumnDefinition>(); 
     }

     public void AddColumnDefinition(string name, Expression<Func<T, string>> propertyExpression, int width)
     {
          /* Add Column Definition with Expression Data */
     }
}

关于c# - 由于泛型类型推断的限制,无法使用泛型来改进 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6203315/

相关文章:

Java 集合接口(interface) addAll() 方法签名

c - 通用 HashMap,不需要的输出 (C)

c# - 如何使用 C# 泛型来简化对 WinRT 设置的访问?

c# - System.Windows.Forms.Timer 未在 Foreach block 内触发 Tick 事件

c# - P4.NET - 如何列出用户的工作区?

c# - 使用 Linq 仅从集合中选择子类

c# - 是否可以在 XP 上运行 .NET 4.5 应用程序?

c# - 当不满足Where子句时,LINQ扩展方法是否可以使用new().Value创建新的KeyValuePair

c# - 计算 Int32 中的前导零

c# - 在 C# webBrowser 控件中调用 Javascript 函数