由于泛型类型推断的限制,不能使用泛型来改进API

时间:2011-06-01 15:01:56

标签: c# .net generics type-inference

我有一个类,用于获取任何类型的对象的集合并创建它的导出(例如Excel电子表格)。我可以选择提供列名和宽度:

以下是该课程的摘要:

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>类。但是,我找不到让编译器推断出T参数中使用的IEnumerable<ColumnDefinition<T>>IEnumerable<T>参数中使用的{{1}}相同的方法。这意味着我不能再使用具有匿名类型集合的类,因为我无法显式指定泛型类型参数。

有人可以找到办法吗?

3 个答案:

答案 0 :(得分:0)

在列定义类中,您可以执行以下操作:

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&gt;,这意味着你的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 */
     }
}

答案 1 :(得分:0)

如果不使用一些黑客来强制进行类型推断,那将会有点棘手。这样的事情怎么样?

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

// ...

public class ColumnDefinition<T>
{
    public string ColumnName { get; private set; }
    public Func<T, object> Selector { get; private set; }
    public int Width { get; private set; }

    public ColumnDefinition(string columnName, Func<T, object> selector, int width)
    {
        ColumnName = columnName;
        Selector = selector;
        Width = width;
    }
}

public static class ColumnDefinitionHelper
{
    public static ColumnDefinition<T> GetColumnDef<T>(this IEnumerable<T> source,
        string columnName, Func<T, object> selector, int width)
    {
        return new ColumnDefinition<T>(columnName, selector, width);
    }
}

public class ObjectExporter
{
    public string Export<T>(
        IEnumerable<T> objects, IEnumerable<ColumnDefinition<T>> columnDefinitions)
    {
        // ...
    }
}

答案 2 :(得分:0)

如果您的更改不是通用的,请不要使用泛型。

如果你不能用在T中实现的接口实例替换T,请不要使用泛型。这没有意义。